如何创build一个Android Spinner作为popup窗口?

我想调出一个微调对话框,当用户点击一个菜单项,让用户select一个项目。

我需要一个单独的对话框,或者我可以直接使用微调吗? 我看到这个链接,提到一个MODE_DIALOG选项,但它似乎没有被定义了。 AlertDialog可能是好的,但所有的选项都说“点击列表中的项目不会closures对话框”,这是我想要的。 任何build议?

理想情况下,代码将类似于屏幕上显示微调器的情况:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_item, items); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); myspinner.setAdapter(adapter); // myspinner.showAsDialog() <-- what i want 

您可以使用警报对话框

  AlertDialog.Builder b = new Builder(this); b.setTitle("Example"); String[] types = {"By Zip", "By Category"}; b.setItems(types, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); switch(which){ case 0: onZipRequested(); break; case 1: onCategoryRequested(); break; } } }); b.show(); 

这将closures对话框,当他们中的一个按你想要的。 希望这可以帮助!

在XML中有选项

 android:spinnerMode="dialog" 

使用这个对话框模式

尝试这个:

 Spinner popupSpinner = new Spinner(context, Spinner.MODE_DIALOG); 

看到这个链接了解更多细节。

MODE_DIALOG和MODE_DROPDOWN在API 11(蜂窝)中定义。 MODE_DIALOG描述了以前平台版本中的常见行为。

android:spinnerMode="dialog"添加一个小属性会在popup窗口中显示微调器的内容。

您可以创build自己的自定义对话框。 这很容易。 如果你想用微调器中的select消除它,然后添加一个OnItemClickListener并添加

 int n = mSpinner.getSelectedItemPosition(); mReadyListener.ready(n); SpinnerDialog.this.dismiss(); 

就像OKbutton的OnClickListener一样。 有一个警告,但是,这是onclick监听器不会触发,如果你重新select默认选项。 您还需要确定button。

从布局开始:

res / layout / spinner_dialog.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/dialog_label" android:layout_height="wrap_content" android:layout_width="fill_parent" android:hint="Please select an option" /> <Spinner android:id="@+id/dialog_spinner" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <Button android:id="@+id/dialogOK" android:layout_width="120dp" android:layout_height="wrap_content" android:text="OK" android:layout_below="@id/dialog_spinner" /> <Button android:id="@+id/dialogCancel" android:layout_width="120dp" android:layout_height="wrap_content" android:text="Cancel" android:layout_below="@id/dialog_spinner" android:layout_toRightOf="@id/dialogOK" /> </RelativeLayout> 

然后,创build类:

src / your / package / SpinnerDialog.java

 public class SpinnerDialog extends Dialog { private ArrayList<String> mList; private Context mContext; private Spinner mSpinner; public interface DialogListener { public void ready(int n); public void cancelled(); } private DialogListener mReadyListener; public SpinnerDialog(Context context, ArrayList<String> list, DialogListener readyListener) { super(context); mReadyListener = readyListener; mContext = context; mList = new ArrayList<String>(); mList = list; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spinner_dialog); mSpinner = (Spinner) findViewById (R.id.dialog_spinner); ArrayAdapter<String> adapter = new ArrayAdapter<String> (mContext, android.R.layout.simple_spinner_dropdown_item, mList); mSpinner.setAdapter(adapter); Button buttonOK = (Button) findViewById(R.id.dialogOK); Button buttonCancel = (Button) findViewById(R.id.dialogCancel); buttonOK.setOnClickListener(new android.view.View.OnClickListener(){ public void onClick(View v) { int n = mSpinner.getSelectedItemPosition(); mReadyListener.ready(n); SpinnerDialog.this.dismiss(); } }); buttonCancel.setOnClickListener(new android.view.View.OnClickListener(){ public void onClick(View v) { mReadyListener.cancelled(); SpinnerDialog.this.dismiss(); } }); } } 

最后,将其用作:

 mSpinnerDialog = new SpinnerDialog(this, mTimers, new SpinnerDialog.SpinnerListener() { public void cancelled() { // do your code here } public void ready(int n) { // do your code here } }); 

这是一个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; } } 

欲了解更多信息,请阅读本文: 如何使Android Spinner选项在对话框中popup

 android:spinnerMode="dialog" // Creating adapter for spinner ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, categories); // Drop down layout style - list view with radio button dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // attaching data adapter to spinner spinner.setAdapter(dataAdapter); 

这是从Android SDK源代码。 正如你所看到的,你有一个特殊的构造函数来创build一个你想使用的指定模式的微调。

希望它会帮助你:)

  /** * Construct a new spinner with the given context's theme, the supplied attribute set, * and default style. <code>mode</code> may be one of {@link #MODE_DIALOG} or * {@link #MODE_DROPDOWN} and determines how the user will select choices from the spinner. * * @param context The Context the view is running in, through which it can * access the current theme, resources, etc. * @param attrs The attributes of the XML tag that is inflating the view. * @param defStyle The default style to apply to this view. If 0, no style * will be applied (beyond what is included in the theme). This may * either be an attribute resource, whose value will be retrieved * from the current theme, or an explicit style resource. * @param mode Constant describing how the user will select choices from the spinner. * * @see #MODE_DIALOG * @see #MODE_DROPDOWN */ public Spinner(Context context, AttributeSet attrs, int defStyle, int mode) { super(context, attrs, defStyle);