Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 禁用AlertDialog按钮?_Android_Android Layout - Fatal编程技术网

Android 禁用AlertDialog按钮?

Android 禁用AlertDialog按钮?,android,android-layout,Android,Android Layout,您好,我有以下两个函数,我想知道是否有可能根据布尔值阻止下面的“正按钮”为真或假(如果用户在EditText中输入了文本或未输入文本) 您可以按如下方式执行禁用操作: public void onClick(DialogInterface dialog, int whichButton) { if(addWord((EditText) addView.findViewById(R.id.titleEdit))){ // Do something, Enable the OK (

您好,我有以下两个函数,我想知道是否有可能根据布尔值阻止下面的“正按钮”为真或假(如果用户在EditText中输入了文本或未输入文本)


您可以按如下方式执行禁用操作:

public void onClick(DialogInterface dialog, int whichButton) {
   if(addWord((EditText) addView.findViewById(R.id.titleEdit))){
      // Do something, Enable the OK (Positive) button
   } else {
      Toast.makeText(ActionBarMain.this, "Nothing entered",
          Toast.LENGTH_LONG).show();
      //Prevent the user to be able to push the "PositiveButton" (Block it)
      AlertDialog myDialog = (AlertDialog)dialog;
      Button button = myDialog.getButton(whichButton);
      button.setOnClickListener(null);
   }
}
您还可以尝试其他方法来阻止按钮,现在您可以访问它了

AlertDialog mAlertDialog = new AlertDialog.Builder(this)
                               .setTitle("Add a Book").setView(addView)
                               .setNegativeButton("Cancel", null);

if(!edittext.getText().toString().equals("")){
    mAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        [...]
                    }
                 });
}

mAlertDialog.show();

像这样的。没有测试它。

如何创建DialogBuilder对象(“AlertDialog.Builder b=new…”而不是“new AlertDialog.Builder…”),然后在if条件下添加肯定按钮?
AlertDialog mAlertDialog = new AlertDialog.Builder(this)
                               .setTitle("Add a Book").setView(addView)
                               .setNegativeButton("Cancel", null);

if(!edittext.getText().toString().equals("")){
    mAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        [...]
                    }
                 });
}

mAlertDialog.show();