开发者

Android - Change the text of the positive button of an AlertDialog

I reuse an AlertDialog box in my android app.

I create a dialog in the onCreateDialog() method and in the onPrepareDialog() method, I try to change the text of the positiveButton using the following code.

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

The onclick listener is开发者_如何学运维 getting changed, but the button text is not changed.

Is it a bug in Android or am I doing something wrong?


One solution is just to force the button to redraw. For example, a button to cancel a lengthy operation might change to 'OK' on complete, e.g.

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


This works for me

@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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜