Android:创建一个具有多个选择选项的弹出窗口 [英] Android: create a popup that has multiple selection options
问题描述
我一直在四处寻找,试图弄清楚如何创建一个有 4 个选项可供选择的弹出窗口或对话框.
I've been searching around trying to figure out how to create a popup or a dialog that has 4 options to choose from.
我在 Android 开发者网站上看到这张图片:
I see this picture on the Android developer site:
有谁知道如何编写类似于右侧的代码?我的文本旁边不需要任何图标,我只需要能够从 4 个选项中进行选择.
Does anyone know how to code up something like the one on the right? I don't need any icons next to my text, I just need to be able to select from 4 options.
推荐答案
您可以创建一个 String
数组,其中包含要在其中显示的选项,然后将该数组传递给 AlertDialog.Builder
使用方法 setItems(CharSequence[], DialogInterface.OnClickListener)
.
You can create a String
array with the options you want to show there and then pass the array to an AlertDialog.Builder
with the method setItems(CharSequence[], DialogInterface.OnClickListener)
.
示例:
String[] colors = {"red", "green", "blue", "black"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(colors, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// the user clicked on colors[which]
}
});
builder.show();
输出(在 Android 4.0.3 上):
The output (on Android 4.0.3):
(不包括背景图.;))
(Background map not included. ;))
这篇关于Android:创建一个具有多个选择选项的弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!