2012-12-18 58 views
0

大家好, 林在比賽中工作,當玩家完成了水平而言,我要求球員在警告對話框上的EditText輸入他的名字:驗證了警報對話框

AlertDialog.Builder builder = new Builder(this); 
     LayoutInflater inflater = getLayoutInflater(); 

     // Inflate and set the layout for the dialog 
     // Pass null as the parent view because its going in the dialog layout 

     builder.setView(inflater.inflate(R.layout.dialog_name, null)) 
     // Add action buttons 
       .setPositiveButton(R.string.alertName, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 

         playerText = (EditText) ((AlertDialog)dialog).findViewById(R.id.username); 
         if(playerText.getText() != null && playerText.getText().length() != 0) 
          Helper.currentPlayer.setName(playerText.getText().toString()); 
         //calculate the score 

         // save in data base 
         PlayerDataSource playerDS = new PlayerDataSource(getApplicationContext()); 
         playerDS.open(); 
         Helper.currentPlayer = playerDS.createPlayer(Helper.currentPlayer.getName(), Helper.currentPlayer.getScore(), 
           Helper.currentPlayer.levels, Helper.currentPlayer.times, LanguageHelper.div); 
         playerDS.close(); 
         saveStmc(); 
         mainScene.setChildScene(choicesMenu, false, true, true); 

        } 
       }) 
       .setTitle("Please Enter Your name") 
       .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 

        } 
       }); 
     alert = builder.create(); 

當玩家進入他的名稱並按確定(肯定按鈕)如果名稱存在或不存在,我將檢查數據庫,如果名稱存在:顯示Toast並且不關閉提醒以寫入其他名稱。怎麼做?

回答

-2
Set the dilaog as cancelable true and overide the onBackPressed() Method if need and 

    AlertDialog.Builder builder = new Builder(this); 
    builder.setCancelable(true); 

// onBackPressed其設置爲true,當你遇到你在對話框

@Override 
public void onBackPressed(){ 
    if(IsNameInserted) 
    super.onBackPressed(); 
} 
+0

對不起Vipin它不工作,我想用戶不能解僱警報,直到輸入有效的名稱 –

+0

可能是你膨脹你查看你需要有你自己的按鈕點擊監聽對話框中的按鈕,如果你的綁定視圖,然後在你的視圖中使用按鈕和 –

0

必要的顯示方式:

AlertDialog.Builder builder = new Builder(this); 
     LayoutInflater inflater = getLayoutInflater(); 

     // Inflate and set the layout for the dialog 
     // Pass null as the parent view because its going in the dialog layout 

     builder.setView(inflater.inflate(R.layout.dialog_name, null)) 
     // Add action buttons 
       .setPositiveButton(R.string.alertName, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 

         playerText = (EditText) ((AlertDialog)dialog).findViewById(R.id.username); 
         if(playerText.getText() != null && playerText.getText().length() != 0) 
          Helper.currentPlayer.setName(playerText.getText().toString()); 
         //calculate the score 

         // save in data base 
         PlayerDataSource playerDS = new PlayerDataSource(getApplicationContext()); 
         playerDS.open(); 

         /////Here check for name in database 
         if (name does not exist) 
         { 

         Helper.currentPlayer = playerDS.createPlayer(Helper.currentPlayer.getName(), Helper.currentPlayer.getScore(), 
           Helper.currentPlayer.levels, Helper.currentPlayer.times, LanguageHelper.div); 
         playerDS.close(); 
         saveStmc(); 
         mainScene.setChildScene(choicesMenu, false, true, true); 
         builder.dismiss() 
         } 
         else 
         { 
          Toast.makeText(getApplicationContext(), "Enter other name..", 
       Toast.LENGTH_SHORT).show(); 
         } 
        } 
       }) 
       .setTitle("Please Enter Your name") 
       .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 

        } 
       }); 
     builder.setCanceledOnTouchOutside(false);    
     alert = builder.create(); 
     builder.show() 
+0

我已經設置** builder.setCanceledOnTouchOutside(false); **但不工作,仍然可以被解僱,當用戶點擊確定 –

0

我已經找到了解決方案,它是基於覆蓋OnClick()AlertDialog的正向按鈕,這裏是解決方案:

 AlertDialog.Builder builder = new Builder(this); 
     LayoutInflater inflater = getLayoutInflater(); 

     // Inflate and set the layout for the dialog 
     // Pass null as the parent view because its going in the dialog layout 

     builder.setView(inflater.inflate(R.layout.dialog_name, null)) 

       .setPositiveButton(R.string.alertName, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 


        } 
       }) 
       .setTitle("Please Enter Your name"); 
     builder = builder.setCancelable(false); 
     alert = builder.create(); 

     alert.setOnShowListener(new OnShowListener() { 

       @Override 
       public void onShow(DialogInterface dialog) { 
        alert = (AlertDialog)dialog; 
        Button b = alert.getButton(AlertDialog.BUTTON_POSITIVE); 
        b.setOnClickListener(new View.OnClickListener() { 

         @Override 
         public void onClick(View view) { 
           playerText = (EditText) ((AlertDialog)alert).findViewById(R.id.username); 
           if(playerText.getText() == null || playerText.getText().length() == 0) 
           { 

            showToastMsg("Please enter valid name"); 
           } 
           else 
           { 

            if(nameIsExist()) 
            { 
             showToastMsg("Name already exist!!"); 
            } 
            else 
            { 
             //Do stuff because every this is OK 
             alert.dismiss(); 
            } 

           } 

         } 
        }); 
       } 
      });