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.
推荐答案
你可以使用要显示的选项创建一个 CharSequence
数组,然后将数组传递给一个 AlertDialog.Builder
与方法 setItems(CharSequence [],DialogInterface.OnClickListener)
。
You can create a CharSequence
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)
.
一个例子:
CharSequence colors[] = new CharSequence[] {"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屋!