4

有没有办法在运行时禁用 AlertDialog 的正按钮,比如在 TextWatcher 中?

AlertDialog.Builder(this)
                    .setTitle(getString(R.string.createvfs))
                    .setView(newVSView_v11)
                    .setPositiveButton(getString(R.string.okay),
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                }
                            })
                    .setNegativeButton(getString(R.string.cancel),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                    // Canceled.
                                }
                            }).show();
4

1 回答 1

7

您需要获取对对话框本身的引用以便稍后对其进行修改,因此您必须稍微更改您的构建器,但是您可以随时调用AlertDialog.getButton()以启用或禁用该按钮。像这样的东西...

//Use create() so you can get the instance back
AlertDialog dialog = AlertDialog.Builder(this)
                .setTitle(getString(R.string.createvfs))
                .setView(newVSView_v11)
                .setPositiveButton(getString(R.string.okay),
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                            }
                        })
                .setNegativeButton(getString(R.string.cancel),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                // Canceled.
                            }
                        }).create();
//Then show it
dialog.show();

/* ...Sometime in the distance future... */
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);

如果你真的想让按钮不可见,那就有点困难了。我目前无法测试setVisibility()按下按钮是否会产生良好的结果......

高温高压

于 2012-04-25T22:06:54.360 回答