4

我在我的 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中的错误还是我做错了什么?

4

2 回答 2

22

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

按钮按钮 = progressDialog.getButton(ProgressDialog.BUTTON1);
button.setText("OK");
button.invalidate();
于 2010-12-02T11:52:49.513 回答
-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;
}
于 2010-07-27T10:34:04.987 回答