0

我尝试EditText在 a 中使用 a Dialog我使用来自 developer.android.com ( http://developer.android.com/guide/topics/ui/dialogs.html )的示例。现在我想在Value按下 时获得 EditText 的positive Button
我的代码

    public Dialog onCreateDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(main_list.this);
    // Get the layout inflater
   final LayoutInflater inflater = main_list.this.getLayoutInflater();


    builder.setView(inflater.inflate(R.layout.serie_add, null))
            // Add action buttons
            .setPositiveButton(R.string.search, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    View promptsView = inflater.inflate(R.layout.serie_add, null);
                    final EditText userInput = (EditText) promptsView
                            .findViewById(R.id.series);

                    Intent intent = new Intent();
                    intent.setClass(main_list.this, add_series.class);
                    intent.putExtra("name", userInput.getText());
                    startActivity(intent);
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                }
            });
    return builder.create();
}
4

2 回答 2

1

如我所见,代码中有两个问题。第一个是您在 onClick 期间膨胀视图,因此显然它是空的。膨胀应该事先完成,然后你才应该在 onClick 事件上注册。其次,你正在做:

userInput.getText() instead of
userInput.getText().toString();
于 2013-10-06T20:09:13.193 回答
0

将您的代码重写为:

public Dialog onCreateDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(main_list.this);
// Get the layout inflater
final LayoutInflater inflater = main_list.this.getLayoutInflater();

View view = inflater.inflate(R.layout.serie_add, null);
final EditText userInput = (EditText) view
                        .findViewById(R.id.series);

builder.setView(view)
        // Add action buttons
        .setPositiveButton(R.string.search, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                View promptsView = inflater.inflate(R.layout.serie_add, null);


                Intent intent = new Intent();
                intent.setClass(main_list.this, add_series.class);
                intent.putExtra("name", userInput.getText().toString());
                startActivity(intent);
            }
        })
        .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

            }
        });
return builder.create();
}
于 2013-10-06T20:05:45.803 回答