Android - 更改AlertDialog的正按钮的文本

时间:2010-07-27 10:24:50

标签: android alertdialog

我在我的Android应用程序中重用了一个AlertDialog框。

我在onCreateDialog()方法和onPrepareDialog()方法中创建了一个对话框,我尝试使用以下代码更改positiveButton的文本。

alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, this.getString(R.string.add), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
          //Handler code
    }
}

onclick侦听器正在更改,但按钮文本未更改。

这是Android中的错误还是我做错了什么?

2 个答案:

答案 0 :(得分:22)

一种解决方案就是强制按钮重绘。例如,取消冗长操作的按钮可能在完成时变为“OK”,例如

Button button = progressDialog.getButton(ProgressDialog.BUTTON1);
button.setText("OK");
button.invalidate();

答案 1 :(得分:-1)

这对我有用

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {

        case DIALOG_ID:
            return AlertDialog.Builder(this).setTitle(R.string.contact_groups_add)
    .setView(addView).setPositiveButton(R.string.ok,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int whichButton) {

                }
            }).setNegativeButton(R.string.cancel,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int whichButton) {
                }
            }).create();
    }
    return null;
}
相关问题