java - Android:如何在自定义 AlertDialog 中检索 Edittext.getText()?

标签 java android layout android-edittext android-alertdialog

该主题解释了我所追求的...我无法从我在 android 中的自定义 View 中检索 EditText。我得到的只是一个空指针异常。 :/ 我已经用注释标记了代码中的问题所在。 ID:s 是正确的,我的 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();
}

提前致谢! 亚历克斯

最佳答案

将对话框转换为 View :

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

关于java - Android:如何在自定义 AlertDialog 中检索 Edittext.getText()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12799751/

相关文章:

java - 创建 <String, List> 的映射时出现问题

java - 接收到的字节数组与发送的字节数组具有不同的值,AES 加密 + IV

java - 获取本质上是原型(prototype)的自动连线字段的新实例的更干净的方法

java - JSONException : Value <br of type java. lang.String 无法转换为 JSONArray

java - 如何将文本移动到 JButton 的底部

java - 将java对象序列化到文件时是否可以在序列化文件的开头插入一个对象

android - TabPageIndicator 和 TitlePageIndicator 之间的实现差异是什么?

Android 回合制多人游戏 - 如何在屏幕关闭时获取比赛更新?

java - Android:我无法通过 id 获取布局对象

容器底部的 CSS Position 元素,无需将其从流程中移除