对话框-如何将Android Spinner创建为弹出窗口?
我想在用户点击菜单项以允许用户选择项目时调出微调器对话框。
我是否需要一个单独的对话框,还是可以直接使用Spinner? 我看到此链接,提到了MODE_DIALOG选项,但似乎不再定义了。 AlertDialog可能没问题,但是所有选项都说“单击列表中的项目不会关闭对话框”,这是我想要的。 有什么建议吗?
理想情况下,代码类似于在屏幕上显示微调框的情况:
ArrayAdapter adapter = new ArrayAdapter(activity,
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
myspinner.setAdapter(adapter);
// myspinner.showAsDialog()
11个解决方案
108 votes
您可以使用警报对话框
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();
当您按需要按下其中一个时,将关闭对话框。 希望这可以帮助!
Nathan Schwermann answered 2019-10-24T15:33:45Z
86 votes
在xml中有选项
android:spinnerMode="dialog"
将此用于对话模式
user2582324 answered 2019-10-24T15:34:22Z
17 votes
试试这个:
Spinner popupSpinner = new Spinner(context, Spinner.MODE_DIALOG);
有关更多详细信息,请参见此链接。
haotang answered 2019-10-24T15:34:50Z
11 votes
MODE_DIALOG和MODE_DIALOG在API 11(蜂窝)中定义。 MODE_DIALOG描述了先前平台版本中的常见行为。
adamp answered 2019-10-24T15:35:17Z
9 votes
添加一个小属性如android:spinnerMode="dialog"将在弹出窗口中显示微调器内容。
San answered 2019-10-24T15:35:45Z
6 votes
您可以创建自己的自定义对话框。 这很容易。 如果要通过微调器中的选择关闭它,则添加OnItemClickListener并添加
int n = mSpinner.getSelectedItemPosition();
mReadyListener.ready(n);
SpinnerDialog.this.dismiss();
就像OnClickListener中的“确定”按钮一样。 不过,有一个警告,就是如果您重新选择默认选项,则onclick侦听器不会触发。 您还需要确定按钮。
从布局开始:
res / layout / spinner_dialog.xml:
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:id="@+id/dialog_label"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Please select an option"
/>
android:id="@+id/dialog_spinner"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
android:id="@+id/dialogOK"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="OK"
android:layout_below="@id/dialog_spinner"
/>
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"
/>
然后,创建类:
src /您的/package/SpinnerDialog.java:
public class SpinnerDialog extends Dialog {
private ArrayList 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 list, DialogListener readyListener) {
super(context);
mReadyListener = readyListener;
mContext = context;
mList = new ArrayList();
mList = list;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_dialog);
mSpinner = (Spinner) findViewById (R.id.dialog_spinner);
ArrayAdapter adapter = new ArrayAdapter (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.DialogListener() {
public void cancelled() {
// do your code here
}
public void ready(int n) {
// do your code here
}
});
Aleadam answered 2019-10-24T15:37:01Z
2 votes
这是一个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选项弹出窗口
Roger Keays answered 2019-10-24T15:37:38Z
2 votes
您可以使用微调器,并将spinnerMode设置为dialog,并将layout_width和layout_height设置为0,这样就不会显示主视图,而仅显示对话框(下拉视图)。 在按钮单击侦听器中调用performClick。
mButtonAdd.setOnClickListener(view -> {
spinnerAddToList.performClick();
});
布局:
android:id="@+id/spinnerAddToList"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:prompt="@string/select_from_list"
android:theme="@style/ThemeOverlay.AppCompat.Light"
android:spinnerMode="dialog"/>
这样的好处是您可以根据需要自定义微调器。
在此处查看我的答案以自定义微调框:对话框模式下微调器的覆盖下拉列表样式
live-love answered 2019-10-24T15:38:25Z
1 votes
android:spinnerMode="dialog"
// Creating adapter for spinner
ArrayAdapter dataAdapter = new ArrayAdapter(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);
nzala answered 2019-10-24T15:38:48Z
0 votes
这是来自Android SDK源代码。如您所见,您有一个特殊的构造函数来创建要使用的指定模式的Spinner。
希望它能对您有所帮助:)
/**
* Construct a new spinner with the given context's theme, the supplied attribute set,
* and default style. mode
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);
Ektos974 answered 2019-10-24T15:39:20Z
0 votes
如果您希望将其显示为全屏弹出窗口,则甚至不需要xml布局。 这是在Kotlin中的操作方法。
val inputArray: Array = arrayOf("Item 1","Item 2")
val alt_bld = AlertDialog.Builder(context);
alt_bld.setTitle("Items:")
alt_bld.setSingleChoiceItems(inputArray, -1) { dialog, which ->
if(which == 0){
//Item 1 Selected
}
else if(which == 1){
//Item 2 Selected
}
dialog.dismiss();
}
val alert11 = alt_bld.create()
alert11.show()
grantespo answered 2019-10-24T15:39:51Z