0

我的场景,主屏幕中有一个按钮,当用户单击该按钮时,会出现一个带有 textedit 和 2 个按钮的对话框。我的问题是,当我尝试从编辑文本中获取值时,似乎什么也没发生,并且该值始终为 NULL。

这是我的代码:我在主要活动中声明对话框

private void popup() {
        AlertDialog.Builder builder;
        AlertDialog alertDialog;

        LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View dialog = inflater.inflate(R.layout.isbn_dialog,
                                       (ViewGroup) findViewById(R.id.layout_root));

                    // The edit text from dialog
        isbnInput = (EditText)dialog.findViewById(R.id.isbn);

        builder = new AlertDialog.Builder(this);
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), isbnInput.getText().toString(), Toast.LENGTH_LONG);
            }
        });

        builder.setView(dialog);
        alertDialog = builder.create();
        alertDialog.setTitle("Enter ISBN Number");
        alertDialog.show();
}

那么如何正确地从对话框编辑文本中获取值?

4

2 回答 2

1

这条线一定是,

 Toast.makeText(getApplicationContext(), isbnInput.getText().toString(), Toast.LENGTH_LONG).show();

您忘记show()在按钮的点击内吐司..

尝试一下..

于 2012-06-02T08:08:24.490 回答
0

我没有看到您对 AlertDialog 的 editext 引用。

试试这个,和你一样,可以肯定的是我可以editText正常

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setMessage(getResources().getString(R.string.my_email));
    final EditText input = new EditText(this);



    final LinearLayout layout = new LinearLayout(this);
    LayoutParams params = new LayoutParams(
            (int) getResources().getDimension(R.dimen.simple_alert_width_normal),
            LayoutParams.WRAP_CONTENT);
    params.setMargins(10, 0, 10, 0);
    layout.addView(input, params);

    alert.setView(layout);
    alert.setPositiveButton(R.string.OK,
            new DialogInterface.OnClickListener() {
            // DO some thing here

            });

    alert.setNegativeButton(R.string.cancel,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.

                }
            });

    alert.show();
于 2012-06-02T08:09:03.383 回答