2014-03-29 44 views
0

我有以下對話框代碼,但問題在於選擇在我按下OK按鈕之前生效。我應該怎麼做才能使選擇在按下OK按鈕後生效?Android對話框不在等待確定按鈕

public void cropSelect() 
{ 
    final CharSequence[] cropType = {"Wheat","Corn","Soybean"}; 
    AlertDialog.Builder builder = new AlertDialog.Builder(MainContext.myContext); 
    builder.setTitle("Choose Crop"); 
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) {} 
    }); 

    builder.setSingleChoiceItems(cropType, -1, new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 

      if("Wheat".equals(cropType[which])){ 
       cropSelect = "Wheat"; 
      } 
      else if("Corn".equals(cropType[which])){ 
       cropSelect = "Corn"; 
      } 
      else if("Soybean".equals(cropType[which])){ 
       cropSelect = "Soybean"; 
      } 

     } 
    }); 
    builder.show(); 

回答

1

您希望保存他們在onClick中選擇的內容,但不會將其設置爲全局的cropSelect變量,直到onClick爲正值按鈕。現在,您只需在觸摸該項目時立即寫入cropSelect變量。

相關問題