2014-11-23 35 views
0

我有一個代碼[貼在下面的正文],用OK和Cancel按鈕顯示數組列表中的項目列表。基本上我想禁用確定按鈕,直到從列表中選擇一個特定的項目。任何人都可以讓我知道我可以如何爲我的代碼做到這一點?Android:禁用setPositiveButton,除非和直到選中項目

代碼:

import java.util.ArrayList; 

import android.support.v7.app.ActionBarActivity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnMultiChoiceClickListener; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.RelativeLayout; 
import android.widget.Toast; 

public class MainActivity extends ActionBarActivity { 

    final CharSequence myList[] = { "Tea", "Coffee", "Milk" }; 
    ArrayList<Integer> selList=new ArrayList<Integer>(); 
    boolean bl[] = new boolean[myList.length]; 
    RelativeLayout rl; 
    String msg =""; 
    final AlertDialog.Builder ad = new AlertDialog.Builder(this); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     rl = (RelativeLayout) findViewById(R.id.myRL); 
     final Button btn = (Button) findViewById(R.id.btnDialog); 
     ad.setTitle("What do you Like ?"); 
     ad.setMultiChoiceItems(myList,bl, new OnMultiChoiceClickListener() { 
      @Override 
      public void onClick(DialogInterface arg0, int arg1, boolean arg2) { 
       if(arg2) 
       { 
        selList.add(arg1); 
       } 
       else if (selList.contains(arg1)) 
       { 
        selList.remove(Integer.valueOf(arg1)); 
       } 
      } 
     }); 
     ad.setPositiveButton("OK", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       msg=""; 
       for (int i = 0; i < selList.size(); i++) { 

        msg=msg+"\n"+(i+1)+" : "+myList[selList.get(i)]; 
       } 

       Toast.makeText(getApplicationContext(), 
         "Total "+ selList.size() +" Items Selected.\n"+ msg , Toast.LENGTH_LONG)  
         .show(); 
      } 
     }); 
     ad.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplicationContext(), 
         "You Have Cancel the Dialog box", Toast.LENGTH_LONG)  
         .show(); 
      } 
     }); 
     btn.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       msg=""; 
       ad.show(); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

回答

0

您可以從AlertDialog使用getButton(Dialog.BUTTON_POSITIVE)訪問OK按鈕。修改啓動對話的OnClickListener讓您禁用確定按鈕,你顯示對話框之前:

btn.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 
     msg=""; 
     AlertDialog dialog = ad.create(); 
     Button okButton = dialog.getButton(Dialog.BUTTON_POSITIVE); 
     okButton.setEnabled(false); 
     dialog.show(); // call show on the AlertDialog, not the Builder 
    } 
}); 

我不知道究竟需要做些什麼,爲您啓用按鈕,但如果它是響應給用戶選擇一個特定的項目,那麼你應該但在您的OnMultiChoiceClickListener啓用代碼:

ad.setMultiChoiceItems(myList,bl, new OnMultiChoiceClickListener() { 
    @Override 
    public void onClick(DialogInterface arg0, int arg1, boolean arg2) { 
     if(arg2) 
     { 
      selList.add(arg1); 
     } 
     else if (selList.contains(arg1)) 
     { 
      selList.remove(Integer.valueOf(arg1)); 
     } 
     if (/* YOUR CODE HERE */) { 
      AlertDialog dialog = (AlertDialog) arg0; 
      Button okButton = dialog.getButton(Dialog.BUTTON_POSITIVE); 
      okButton.setEnabled(true); 
     } 
    } 
}); 
+0

謝謝你的答覆。我嘗試了你的建議,但我的應用程序崩潰。我在這一行得到一個空指針異常: 最終AlertDialog.Builder ad = new AlertDialog.Builder(this); – Var 2014-11-23 07:16:22

+0

嗯。嘗試在onCreate函數中移動該行。 Builder對象通常只是臨時對象,一旦實際創建對話框後就不需要了。如果這不起作用,那麼請從崩潰中發佈堆棧,以便我可以看到什麼是空的。 – Bruce 2014-11-23 07:21:32

相關問題