带有对话框的Android微调器

时间:2014-02-25 20:34:48

标签: android dialog spinner

我不希望它是dropdown。我想点击微调器,打开他dialogbox。 怎么办呢?
我在微调器的ontouch下面实现了我打开对话框...但是它也会打开下拉列表 我想禁用下拉列表并在点击时实施..

final Spinner serverSpinner = (Spinner) 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(new 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();
} 

2 个答案:

答案 0 :(得分: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是包含Spinner mySpinner的弹出式布局

public class OnSpinnerItemClicked implements OnItemSelectedListener {

    @Override
    public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
        Toast.makeText(parent.getContext(), "Clicked : " +
                parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();


    }

    @Override
    public void onNothingSelected(AdapterView parent) {
        // Do nothing.
    }
}

How to make an alert dialog with a spinner

找到答案

答案 1 :(得分:1)

这是一个Spinner子类,它覆盖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