当用户点击菜单项以允许用户选择项目时,我想调出微调器对话框。
我是否需要单独的对话框,还是可以直接使用Spinner?我看到this link,提到了一个MODE_DIALOG选项,但它似乎不再被定义了。 AlertDialog可能没问题,但所有选项都说“单击列表中的项目不会忽略对话框”这就是我想要的。有什么建议吗?
理想情况下,代码类似于在屏幕上显示微调器的情况:
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();
当按下其中一个按钮时,这将关闭对话框,就像你想要的那样。希望这可以帮助!
这是来自Android SDK的源代码。正如您所看到的,您有一个特殊的构造函数来创建一个具有您想要使用的指定模式的Spinner。
希望它能帮到你:)
/**
* 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);
如果要将其显示为全屏弹出窗口,则甚至不需要xml布局。这是Kotlin如何做到的。
val inputArray: Array<String> = 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()
在xml中有选项
android:spinnerMode="dialog"
用于对话模式
MODE_DIALOG
和MODE_DROPDOWN
在API 11(Honeycomb)中定义。 MODE_DIALOG
描述了以前平台版本中的常见行为。
添加一个小属性作为android:spinnerMode="dialog"
将在弹出窗口中显示微调内容。
您可以创建自己的自定义对话框。这很容易。如果你想通过微调器中的选择来解除它,那么添加一个OnItemClickListener
并添加
int n = mSpinner.getSelectedItemPosition();
mReadyListener.ready(n);
SpinnerDialog.this.dismiss();
就像在OK按钮的OnClickListener中一样。但是,有一点需要注意,如果重新选择默认选项,则onclick侦听器不会触发。你也需要OK按钮。
RES /布局/ 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>
SRC /你/包/ 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.DialogListener() {
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;
}
}
有关更多信息,请阅读本文:How To Make Android Spinner Options Popup In A Dialog
您可以使用微调器并将spinnerMode设置为对话框,并将layout_width和layout_height设置为0,以便主视图不显示,只显示对话框(下拉视图)。在按钮单击侦听器中调用performClick。
mButtonAdd.setOnClickListener(view -> {
spinnerAddToList.performClick();
});
布局:
<Spinner
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"/>
这样做的好处是您可以任意方式自定义您的微调器。
请在此处查看我的答案以自定义微调器:Overriding dropdown list style for Spinner in Dialog mode
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);