2012-08-02 48 views
1

我目前正在嘗試保留一個警告對話框,並在屏幕上顯示編輯文本。像往常一樣,有好的和取消按鈕,我想爲流程添加一點錯誤處理。如果用戶在編輯文本框中沒有輸入任何文本並嘗試按下OK,我想要一條Toast消息彈出來通知他們錯誤。該對話框應該仍然顯示,因爲如果他們想按下OK,他們仍然需要輸入內容。如何使用顯示的編輯文本保留警報對話框

這是我到目前爲止。順便說一句,基於用戶從下拉列表中選擇的內容彈出對話框。獲得對話框熬夜的任何幫助將不勝感激!

這裏是我的代碼:

regionSpinner.setOnItemSelectedListener(new OnItemSelectedListener() 
    { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View v, int position, long rowId) 
     { 
      regionSelect = regionSpinner.getItemAtPosition(position).toString(); 
      regionInteger = (int) regionSpinner.getItemIdAtPosition(position); 




      chosenRegion = regionSpinner.getSelectedItem().toString(); 

      if (chosenRegion.equals(ENTER_OPTION)) 

      { 
       final AlertDialog.Builder alert = new AlertDialog.Builder(LoginActivity.this); 

       alert.setTitle(TITLE_DIALOG); 

       // Set an EditText view to get user input 
       final EditText input = new EditText(LoginActivity.this); 
       alert.setView(input); 

       alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
       { 
        public void onClick(DialogInterface dialog, int whichButton) 
        { 
         if(input.getText().toString().length() > 0 && input != null) 
         { 
         textValueForEnteredRegion = input.getText().toString(); 
         Toast.makeText(getApplicationContext(), "Input Accepted", Toast.LENGTH_SHORT).show(); 


         dialog.dismiss(); 
         //added soft keyboard code to make keyboard go away. 
         getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 


         } 
         else 
         { 

          Toast.makeText(getApplicationContext(), "You must enter a selection.", Toast.LENGTH_SHORT).show(); 

         } 
        } 


       }); 



       alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
       { 
        public void onClick(DialogInterface dialog, int whichButton) 
        { 
         // Canceled. 
         dialog.dismiss(); 

         //added soft keyboard code to make keyboard go away. 
         getWindow().setSoftInputMode(
           WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 


        } 
       }); 

       alert.show(); 



      } 


     } 



     @Override 
     public void onNothingSelected(AdapterView<?> arg0) 
     { 

     } 


    }); 

回答

2

覆蓋按鈕的onClick方法。

參見:Android SDK override AlertDialog events

AlertDialog.Builder build=new AlertDialog.Builder(this); 
build.setMessage("This is a message, dawg.") 
.setPositiveButton("Awesomesauce.", new OnClickListener() 
{ 
    public void onClick(DialogInterface dialog, int id) 
    { 
     //Change stuff in here so that dialog.dismiss() is not called. 
     //dialog.dismiss(); //closes the window 
    } 
}; 
AlertDialog myAlert=build.create(); 
myAlert.show(); 

編輯:看起來你已經有這個...你只需要在你的onPostitiveButton的onClick方法來擺脫dialog.dismiss(的),並把有條件的周圍是檢查您的EditText是否包含一些文本。

+0

哇,我帶走了駁回呼叫,並通過自身仍然關閉對話框..... – Tastybrownies 2012-08-02 16:11:14

1

使AlertDialog不可取消你需要調用這個方法alert.setCancelable(false);

並顯示敬酒消息,如果EDITTEXT是空的,你只需要檢查EDITTEXT的內容,如果它是空的告訴他們舉杯別人做你想做的,你的代碼應該看起來像下面的代碼。

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
       { 
        public void onClick(DialogInterface dialog, int whichButton) 
        { 
         if(!input.getText().toString().equals("")) // if not empty do what u were doing 
         { 
         textValueForEnteredRegion = input.getText().toString(); 
         Toast.makeText(getApplicationContext(), "Input Accepted", Toast.LENGTH_SHORT).show(); 


         dialog.dismiss(); 
         //added soft keyboard code to make keyboard go away. 
         getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 


         } else{ // here edittext is empty.. show user a toast and return, 
Toast.makeText(getApplicationContext(), "Input is empty", Toast.LENGTH_SHORT).show(); 


} 
+0

我繼續已將此添加到警報對話框的標題設置的區域,並沒有做任何事情。是否設置了自然警報對話框功能,因此如果按下任何按鈕,對話框將消失? – Tastybrownies 2012-08-02 16:00:53

+0

我在第一個代碼中已經有了。 – Tastybrownies 2012-08-02 16:06:04

相關問題