安卓:如何在自定义AlertDialog中获取Edittext.getText()?

11
主题说明了我要做什么...我无法从我的自定义视图中检索出EditText,所有我得到的都是NullPointerException异常。:/ 我已经使用注释标记了代码中的问题所在。 ID是正确的,我的XML布局只包含两个EditText属性的简单RelativeLayout。 显然,我在这里缺少一些微不足道的东西,但我已经盯着代码看了将近2个小时,没有解决这个问题,所以我想我应该尝试一下SO。
protected void showLoginDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    // Get the layout inflater
    LayoutInflater inflater = this.getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.activity_login, null))
    // Add action buttons
    .setPositiveButton(R.string.login, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {

            /* ERROR HERE! */
            EditText uName, passWord;
            uName = (EditText) findViewById(R.id.login_username);
            passWord = (EditText) findViewById(R.id.login_password);

            Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString());
            /* STOP */

            if(the_view.getSocketTask().isConnected) {
                the_view.getSocketTask().send_command("LOGIN ");
            } else {
                showToast("Not connected!");
            }
        }
    })
    .setNegativeButton(R.string.cancel,  new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });      
    builder.create().show();
}

编辑:

根据建议,以下代码是可行的!再次感谢!

protected void showLoginDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    // Get the layout inflater
    LayoutInflater inflater = this.getLayoutInflater();



    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.activity_login, null))
    // Add action buttons
    .setPositiveButton(R.string.login, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Dialog f = (Dialog) dialog;
            /* ERROR HERE! */
            EditText uName, passWord;
            uName = (EditText) f.findViewById(R.id.login_username);
            passWord = (EditText) f.findViewById(R.id.login_password);

            Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString());
            /* STOP */

            if(the_view.getSocketTask().isConnected) {
                the_view.getSocketTask().send_command("LOGIN ");
            } else {
                showToast("Not connected!");
            }
        }
    })
    .setNegativeButton(R.string.cancel,  new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });      
    builder.create().show();
}

提前感谢! Alex

3个回答

19

将对话框转换为视图:

View v_iew=inflater.inflate(R.layout.activity_login, null)) ;
builder.setView(v_iew);
然后替换:
        uName = (EditText) findViewById(R.id.login_username); 
        passWord = (EditText) findViewById(R.id.login_password);

使用

        uName = (EditText) v_iew.findViewById(R.id.login_username);
         passWord = (EditText) v_iew.findViewById(R.id.login_password); 

2
@Hari,“dialog”参数的类型为“DialogInterface”,该类型没有该方法,需要将其转换为“Dialog”。 - user
1
谢谢大家!Dialog f = (Dialog) dialog; 解决了我的问题! - Widerberg

2
在EditText的情况下,您应该实现TextWatcher。通常使用editText.getText()是一个非常糟糕的想法。
这里有一个非常简单的示例代码,用于包含EditText作为布局一部分的自定义对话框。还有一个按钮需要在同一布局上,当点击时将显示您刚刚输入的文本。玩得开心!
        final String inputString = null;
        final Dialog dialog = new Dialog(YourActivityName.this);
        dialog.setContentView(R.layout.custom_dialog_layout);
        EditText editText = (EditText) dialog.findViewById(R.id.id_of_edit_text);
        Button done = (Button) dialog.findViewById(R.id.done);
        dialog.show();
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                inputString = s.toString();

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });

        done.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(YourActivityName.this, inputString, Toast.LENGTH_SHORT);
                dialog.dismiss();

            }
        });

谢谢!经过一些小修改后,它的工作效果非常好!=) - Widerberg

2

无法投票,因此创建新答案:我花了几个小时才弄清楚这个问题;添加修饰符最终解决了它 - 就像您已经发现的那样。

不起作用的代码: final Dialog dialog = new Dialog(this); ... keyInput = (EditText) findViewById(R.id.key_input);

有效的代码: final Dialog dialog = new Dialog(this); ... keyInput = (EditText) dialog.findViewById(R.id.key_input);


感谢您提供简单的答案。我真的不想实现我的第三个嵌套层Java监听器。我已经被C#宠坏了。 - johnc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接