11

我一直在开发一个Android应用程序。

我想在用户按下 OK 按钮后隐藏它,因为在进行计算时对话框窗口将在前台停留几秒钟。

这是代码:

    new AlertDialog.Builder(this)
    .setMessage("This may take a while")
    .setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {                
        @Override
        public void onClick(DialogInterface dialog, int which) {
                       // hide the OK button - how?
                       // a lot of computation
        }
    })
    .show(); 

我怎样才能做到这一点?

PS:我对处理计算的更高级技术(例如:进度对话框、多线程)不感兴趣。

谢谢。

4

4 回答 4

22
.setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
         ((AlertDialog)dialog).getButton(which).setVisibility(View.INVISIBLE);
         // the rest of your stuff
    }
})
于 2010-11-27T22:51:08.863 回答
1
setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.dismiss();

dialog在哪里DialogInterface

于 2017-04-10T08:11:14.180 回答
1

对于尝试禁用 上的“肯定”按钮的其他任何人AlertDialog,这些解决方案中的许多现在似乎都不起作用 - 一旦调用 ,您就无法获取对该按钮的引用createAlertDialog.Builder它只会返回 null。

所以它应该可用的一个地方是 in onResume,所以我让它工作的方式是这样的:

var positiveButton: Button? = null

override fun onResume() {
    super.onResume()
    if (positiveButton == null) {
        positiveButton = (dialog as AlertDialog).getButton(AlertDialog.BUTTON_POSITIVE)
    }
}

这样一来,无论何时您的片段运行,您都应该有一个可用的参考,因此您可以positiveButton.isEnabled = false在需要禁用它时调用。

请注意您的状态,重新创建的片段可能有一些复选框或其他内容,但它还没有那个positiveButton引用,它会在onResume. 因此,如果您需要进行任何初始化(例如在用户选择一个选项之前禁用按钮),请确保调用一个检查当前状态并决定是否应启用按钮的函数。不要以为它只是开始空白!

于 2020-05-22T18:22:24.403 回答
-3

您可以将按钮的可见性设置为不可见。

ok.setVisibility(View.INVISIBLE);
于 2010-11-27T13:07:04.233 回答