根据文件,有三种列表可以与AlertDialog:传统单选列表
持久单选择列表(单选按钮)
持久性多项选择列表(复选框)
我将给出下面每一个例子。
传统单选列表
制作传统单一选择列表的方法是使用setItems.
Java版本// setup the alert builderAlertDialog.Builder builder = new AlertDialog.Builder(context);builder.setTitle("Choose an animal");// add a listString[] animals = {"horse", "cow", "camel", "sheep", "goat"};builder.setItems(animals, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0: // horse
case 1: // cow
case 2: // camel
case 3: // sheep
case 4: // goat
}
}});// create and show the alert dialogAlertDialog dialog = builder.create();dialog.show();
不需要OK按钮,因为一旦用户单击列表项控件,就会返回到OnClickListener.
Kotlin版本// setup the alert builderval builder = AlertDialog.Builder(context)builder.setTitle("Choose an animal")// add a listval animals = arrayOf("horse", "cow", "camel", "sheep", "goat")builder.setItems(animals) { dialog, which ->
when (which) {
0 -> { /* horse */ }
1 -> { /* cow */ }
2 -> { /* camel */ }
3 -> { /* sheep */ }
4 -> { /* goat */ }
}}// create and show the alert dialogval dialog = builder.create()dialog.show()
单选按钮列表
单选按钮列表相对于传统列表的点是用户可以看到当前设置的内容。制作单选按钮列表的方法是使用setSingleChoiceItems.
Java版本// setup the alert builderAlertDialog.Builder builder = new AlertDialog.Builder(context);builder.setTitle("Choose an animal");// add a radio button listString[] animals = {"horse", "cow", "camel", "sheep", "goat"};int checkedItem = 1; // cowbuilder.setSingleChoiceItems(animals, checkedItem, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// user checked an item
}});// add OK and Cancel buttonsbuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// user clicked OK
}});builder.setNegativeButton("Cancel", null);// create and show the alert dialogAlertDialog dialog = builder.create();dialog.show();
我在这里硬编码了选中的项,但是您可以在实际项目中使用类成员变量来跟踪它。
Kotlin版本// setup the alert builderval builder = AlertDialog.Builder(context)builder.setTitle("Choose an animal")// add a radio button listval animals = arrayOf("horse", "cow", "camel", "sheep", "goat")val checkedItem = 1 // cowbuilder.setSingleChoiceItems(animals, checkedItem) { dialog, which ->
// user checked an item}// add OK and Cancel buttonsbuilder.setPositiveButton("OK") { dialog, which ->
// user clicked OK}builder.setNegativeButton("Cancel", null)// create and show the alert dialogval dialog = builder.create()dialog.show()
复选框列表
生成复选框列表的方法是使用setMultiChoiceItems.
Java版本// setup the alert builderAlertDialog.Builder builder = new AlertDialog.Builder(context);builder.setTitle("Choose some animals");// add a checkbox listString[] animals = {"horse", "cow", "camel", "sheep", "goat"};boolean[] checkedItems = {true, false, false, true, false};builder.setMultiChoiceItems(animals, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// user checked or unchecked a box
}});// add OK and Cancel buttonsbuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// user clicked OK
}});builder.setNegativeButton("Cancel", null);// create and show the alert dialogAlertDialog dialog = builder.create();dialog.show();
在这里,我对列表中已经检查过的项目进行了硬编码。更有可能的是,您希望在ArrayList..见文档示例更多细节。还可以将选中的项设置为null如果你总是想让一切都开始不受检查。
Kotlin版本// setup the alert builderval builder = AlertDialog.Builder(context)builder.setTitle("Choose some animals")// add a checkbox listval animals = arrayOf("horse", "cow", "camel", "sheep", "goat")val checkedItems = booleanArrayOf(true, false, false, true, false)builder.setMultiChoiceItems(animals, checkedItems) { dialog, which, isChecked ->
// user checked or unchecked a box}// add OK and Cancel buttonsbuilder.setPositiveButton("OK") { dialog, which ->
// user clicked OK}builder.setNegativeButton("Cancel", null)// create and show the alert dialogval dialog = builder.create()dialog.show()
注记为
context在上面的代码中,不要使用
getApplicationContext()否则你会得到一个
IllegalStateException(见原因)。相反,获取对活动上下文的引用,如
this.
还可以使用以下方法从数据库或其他源填充列表项。或或通过一个
Cursor或
ListAdapter进入
setSingleChoiceItems或
setMultiChoiceItems.
如果列表比屏幕上的长度长,那么对话框将自动滚动它。如果你有一个很长的名单,我想你应该做一个带着
为了测试上面的所有示例,我只有一个简单的项目,只有一个按钮,而不是单击时显示的对话框:import android.support.v7.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
}
public void showAlertDialogButtonClicked(View view) {
// example code to create alert dialog lists goes here
}}
相关