2014-02-25 136 views
1

但我不希望它是dropdown.I想點擊微調,要打開他對話框。 這怎麼辦? 什麼我實現如下微調的ontouch我打開對話框......然而,它也打開下拉菜單,以及 我想禁用下拉列表,並實現它的點擊..Android微調對話框

最後微調serverSpinner =(微調)findViewById(R .id.serverSpinner); String [] array_spinner = new String [2]; array_spinner [0] =「hoho」; array_spinner [1] =「lala」; ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item,array_spinner); serverSpinner.setAdapter(adapter); serverSpinner.setOnTouchListener(新OnTouchListener(){

  @Override 
      public boolean onTouch(View arg0, MotionEvent arg1) { 
       // TODO Auto-generated method stub 
       showSpinner(); 
       return false; 
      } 

    }); 
   protected void showSpinner() 
      { 
     AlertDialog.Builder b = new Builder(this); 
     String[] options = {"hoho","lala"}; 
 b.setTitle("Select an option"); 
       b.setSingleChoiceItems(options, -1,new DialogInterface.OnClickListener(){ 
        public void onClick(DialogInterface dialog, int which) 
        { 

       dialog.dismiss(); 
       switch(which){ 
       case 0: 
       { 
        break; 
       } 
       case 1: 
       { 
        break; 
       } 

      }} 

        }); 
       b.setPositiveButton("CANCEL", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 

        dialog.dismiss(); 

        } 
     }); 


       b.show();} 
        } 
+0

我不斷收到這個「不要叫setOnClickListener的一個AdapterView – user3278732

回答

1
LayoutInflater li = LayoutInflater.from(context); 

      View promptsView = li.inflate(R.layout.my_dialog_layout, null); 

      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 

      alertDialogBuilder.setView(promptsView); 

      // set dialog message 

      alertDialogBuilder.setTitle("My Dialog.."); 
      alertDialogBuilder.setIcon(R.drawable.ic_launcher); 
      // create alert dialog 
      final AlertDialog alertDialog = alertDialogBuilder.create(); 

      final Spinner mSpinner= (Spinner) promptsView 
        .findViewById(R.id.mySpinner); 
      final Button mButton = (Button) promptsView 
        .findViewById(R.id.myButton); 

      // reference UI elements from my_dialog_layout in similar fashion 

      mSpinner.setOnItemSelectedListener(new OnSpinnerItemClicked()); 

      // show it 
      alertDialog.show(); 
      alertDialog.setCanceledOnTouchOutside(false); 

其中my_dialog_layout是你包含了微調mySpinner

彈出佈局

答案在How to make an alert dialog with a spinner

+0

這個心不是我想要的東西及其對iteme選擇監聽器。但是我做了一個解決方法,我把微調在linearlayout ..讓微調unclikable ..並點擊o flinearlayout我打開了警報對話框。什麼你告訴我,打開微調和選擇彈出對話框(不是我想要的) – user3278732

1

發現這裏是一個微調子類覆蓋performClick()來顯示一個對話框,而不是下拉。不需要XML。試試看,讓我知道它是否適合你。

public class DialogSpinner extends Spinner { 
    public DialogSpinner(Context context) { 
     super(context); 
    } 

    @Override 
    public boolean performClick() { 
     new AlertDialog.Builder(getContext()).setAdapter((ListAdapter) getAdapter(), 
      new DialogInterface.OnClickListener() { 
       @Override public void onClick(DialogInterface dialog, int which) { 
        setSelection(which); 
        dialog.dismiss(); 
       } 
      }).create().show(); 
     return true; 
    } 
} 

欲瞭解更多信息,請閱讀這篇文章:。How To Make Android Spinner Options Popup In A Dialog