从PopUpWindow获取editText输入

时间:2013-05-13 21:38:59

标签: android

我想知道如何从弹出窗口中的editText框中获取输入。我试过两种方法。一个有布局充气器,一个没有,但我得到“”或null即使我在盒子里输入了东西

这个返回“”:

// get the username and password inputs
        View inflatedView = getLayoutInflater().inflate(R.layout.login_popup, null);
        EditText usernameInput = (EditText) inflatedView.findViewById(R.id.username_login_input);
        EditText passwordInput = (EditText) inflatedView.findViewById(R.id.password_login_input);

        final String usernameString = usernameInput.getText().toString();
        final String passwordString = passwordInput.getText().toString();

这个返回null:

// get the username and password inputs
        EditText usernameInput = (EditText) findViewById(R.id.username_login_input);
        EditText passwordInput = (EditText) findViewById(R.id.password_login_input);

        final String usernameString = usernameInput.getText().toString();
        final String passwordString = passwordInput.getText().toString();

我试图从login_popup.xml获取它,而不是从活动

生成的

这是代码来自

的地方

修改

LayoutInflater layoutInflater = 
                 (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        View popupView = layoutInflater.inflate(R.layout.login_popup, null);
        ppw = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
        ViewGroup parentLayout = (ViewGroup) findViewById(R.id.title_page_layout);
        // set the position and size of popup
        ppw.showAtLocation(parentLayout, Gravity.CENTER, 10, 20);

1 个答案:

答案 0 :(得分:5)

当您的弹出窗口关闭时,请尝试以下操作:

View contentView = ppw.getContentView();
EditText usernameInput = (EditText) contentView.findViewById(R.id.username_login_input);
EditText passwordInput = (EditText) contentView.findViewById(R.id.password_login_input);
final String usernameString = usernameInput.getText().toString();
final String passwordString = passwordInput.getText().toString();

我认为您当前的方法是膨胀新视图,而不是从弹出窗口中获取现有视图。