简体   繁体   中英

getting editText input from PopUpWindow

I was wondering how I could get the input from an editText box in a popupwindow. I tried 2 ways. One with a layout inflator and one without but I either get "" or null even though i typed something into the box

This one returns the "" :

// 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();

This one returns 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();

I am trying to get it from login_popup.xml which is not generated from an activity

This is where the code is from

EDIT

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);

Try the following instead when your popup window is closed:

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();

I think your current approach is inflating new views, rather than obtaining the existing views from the popup window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM