2012-12-07 79 views
1

例子:Android AlertDialog如何阻止第二次点击确定按钮?

System.out.println("in!"); 
AlertDialog.Builder dialog = new AlertDialog.Builder(this); 
    dialog.setMessage("test!!!"); 
    dialog.setPositiveButton(R.string.dialog_ok, 
     new OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       System.out.println("Only one click!"); 
      } 
    }); 
    dialog.show(); 

日期:
了!
in!
只需点击一下!
只需点击一下!
只需点击一下!
只需点击一下!

+1

你想在第一次点击后禁用按钮或什么? – kosa

+0

我想做出不可能的第二次点击。 – user1879118

+1

这是坏的设计,对话框不应该这样... – JoxTraex

回答

4

获取按钮(正面)并将其设置为false。

System.out.println("in!"); 
AlertDialog.Builder dialog = new AlertDialog.Builder(this); 
dialog.setMessage("test!!!"); 
dialog.setPositiveButton(R.string.dialog_ok, 
    new OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      // disable on 1st click; 
      final AlertDialog alertDialog = (AlertDialog)dialog; 
      alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false); 
      System.out.println("Only one click!"); 
     } 
}); 
dialog.show(); 
0

您需要禁用它。我推荐一个标志,它必须存储在课程级别。

Boolean hasBeenClicked=false; 


System.out.println("in!"); 
AlertDialog.Builder dialog = new AlertDialog.Builder(this); 
    dialog.setMessage("test!!!"); 
    dialog.setPositiveButton(R.string.dialog_ok, 
     new OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       if (!hasBeenClicked) 
       { 
        hasBeenClicked=true; 
        System.out.println("Only one click!"); 
       } 
      } 
    }); 
    dialog.show(); 
2

我不明白为什么有人可以在alertdialog上点击第二次,因为按钮在执行某些操作后应该关闭对话框。 为什么你不关闭对话框 dialog.dismiss() ?

+1

在对话被实际解除之前,该操作可能会排队。如果您进行任何需要很短时间的操作,您可以在实际发生解雇之前再次点击该按钮,这将触发第二次回叫。 – stuckj

相关问题