开发者

Android: Cannot read text in EditBox inside a DialogBox

I want to get user input using a pop-up dialog box. I am able to show the dialog box with an EditText, and the user can enter the input. But when I try to get this value, I just get a null.

It's a simple enough code. I'm probably missing something really elementary here. Not sure what!

private void getUserInput(String prompt) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(prompt);
    final EditText inpu开发者_JAVA百科tBox = new EditText(this);
    builder.setView(inputBox);
    builder.setPositiveButton(android.R.string.ok,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int whichButton) {
                    String str = "";
                    Log.d(DEBUG_TAG, "OK button clicked");
                    if (inputBox.getText() != null) { //PROBLEM: why is inputBox.getText null?? 
                        str = "null";
                    } else {
                        str = inputBox.getText().toString();
                    }
                    Log.d(DEBUG_TAG, "input text >>>" + str);
                    dialog.dismiss();
                }
            });
    builder.show();
}


Where you've got

if (inputBox.getText() != null)

you're setting str to null, but checking inputBox.getText() is NOT null; shouldn't that != be == null? (Although, really, you should probably be checking inputBox.getText().equals("").)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜