【问题标题】:Android: How do I retrieve Edittext.getText() in custom AlertDialog?Android:如何在自定义 AlertDialog 中检索 Edittext.getText()?
【发布时间】:2012-10-09 12:11:33
【问题描述】:

该主题解释了我所追求的...我无法从我在 android 中的自定义视图中检索 EditText。我得到的只是一个空指针异常。 :/ 我已经用 cmets 标记了代码中的问题所在。 ID:s 是正确的,我的 XML 布局是一个简单的 RelativeLayout,其中包含两个 EditText 属性。 显然我在这里遗漏了一些微不足道的东西,但我现在盯着代码看了将近 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();
}

提前致谢! 亚历克斯

【问题讨论】:

    标签: java android layout android-edittext android-alertdialog


    【解决方案1】:

    将对话框转换为视图:

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

    【讨论】:

    • @Hari dialog 参数的类型为 DialogInterface,它没有该方法,需要转换为 Dialog
    • 谢谢你们! Dialog f = (Dialog) dialog;解决了我的问题!
    【解决方案2】:

    (不能投票,所以创建新答案:花了我几个小时才弄清楚;添加限定符最终解决了它 - 就像你已经想通的那样)

    不起作用: 最终对话框对话框 = 新对话框(此); ... keyInput = (EditText) findViewById(R.id.key_input);

    作品: 最终对话框对话框 = 新对话框(此); ... keyInput = (EditText) dialog.findViewById(R.id.key_input);

    【讨论】:

    • 感谢您提供简单的答案。我真的不想实现我的第三层嵌套 Java 侦听器。我被 C# 宠坏了
    【解决方案3】:

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

    【讨论】:

    • 谢谢!经过一些小的修改后就像一个魅力! =)
    猜你喜欢
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多