11

该主题解释了我所追求的......我无法从我在 android 中的自定义视图中检索 EditText。我得到的只是一个空指针异常。:/ 我已经用注释标记了代码中的问题所在。ID:s 是正确的,我的 XML 布局是一个简单的 RelativeLayout,其中包含两个 EditText 属性。显然我在这里遗漏了一些微不足道的东西,但是我现在已经盯着代码看了将近 2 个小时而没有解决这个问题,所以我想我会尝试一下。

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

提前致谢!亚历克斯

4

3 回答 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); 
于 2012-10-09T12:14:27.470 回答
2

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

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

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

于 2014-01-03T06:21:59.890 回答
1

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

            }
        });
于 2012-10-09T12:24:14.170 回答