2012-11-15 5 views
0

안녕하세요 나는 다음과 같은 2 가지 기능을 가지고 있으며 "Positivebutton"을 부울 값에 따라 차단하는 것이 가능한지 궁금해합니다 (사용자가 EditText에 텍스트를 입력했는지 여부).AlertDialog 버튼을 비활성화 하시겠습니까?

private void add() { 
      final View addView = getLayoutInflater().inflate(R.layout.add, null); 
      new AlertDialog.Builder(this).setTitle("Add a Book").setView(addView) 
        .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
         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) 
} 


         } 
        }).setNegativeButton("Cancel", null).show(); 
     } 

     private boolean addWord(EditText title){ 
      String mDisplaySting = title.getText().toString(); 
      if(mDisplaySting.matches("")){ 
       Log.i(TAG,"null"); 
       return false;  
      } 
      return true; 
     } 
+0

DialogBuilder 개체 ("new AlertDialog.Builder ..."대신 "AlertDialog.Builder b = new ...")를 만들고 if-condition에 양수 버튼을 나중에 추가 하시겠습니까? – Prexx

답변

0
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(); 

이와 비슷한 것. 그것을 테스트하지 않았다.

0

다음과 같이 사용 불가능하게 작업을 수행 할 수 있습니다

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); 
    } 
} 

당신은 또한 당신이 그것을에 액세스 할 수있는 지금과 같은 버튼을 차단하는 다른 방법을 실험 할 수 있습니다.

관련 문제