如何在Android Alert Dialog中显示列表视图?

在Android应用程序中,我想在AlertDialog中显示自定义列表视图。

我怎样才能做到这一点?

用于以下代码在AlertDialog中显示自定义列表

AlertDialog.Builder builderSingle = new AlertDialog.Builder(DialogActivity.this); builderSingle.setIcon(R.drawable.ic_launcher); builderSingle.setTitle("Select One Name:-"); final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(DialogActivity.this, android.R.layout.select_dialog_singlechoice); arrayAdapter.add("Hardik"); arrayAdapter.add("Archit"); arrayAdapter.add("Jignesh"); arrayAdapter.add("Umang"); arrayAdapter.add("Gatti"); builderSingle.setNegativeButton("cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String strName = arrayAdapter.getItem(which); AlertDialog.Builder builderInner = new AlertDialog.Builder(DialogActivity.this); builderInner.setMessage(strName); builderInner.setTitle("Your Selected Item is"); builderInner.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int which) { dialog.dismiss(); } }); builderInner.show(); } }); builderSingle.show(); 

您可以使用自定义对话框。

自定义对话框布局 list.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ListView android:id="@+id/lv" android:layout_width="wrap_content" android:layout_height="fill_parent"/> </LinearLayout> 

在你的活动中

 Dialog dialog = new Dialog(Activity.this); dialog.setContentView(R.layout.list) ListView lv = (ListView ) dialog.findViewById(R.id.lv); dialog.setCancelable(true); dialog.setTitle("ListView"); dialog.show(); 

编辑:

使用alertdialog

 String names[] ={"A","B","C","D"}; AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this); LayoutInflater inflater = getLayoutInflater(); View convertView = (View) inflater.inflate(R.layout.custom, null); alertDialog.setView(convertView); alertDialog.setTitle("List"); ListView lv = (ListView) convertView.findViewById(R.id.listView1); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names); lv.setAdapter(adapter); alertDialog.show(); 

custom.xml

 <?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listView1" android:layout_width="fill_parent" android:layout_height="fill_parent" > </ListView> 

快照

在这里输入图像说明

根据文档 , AlertDialog可以使用三种列表:

  1. 传统的单选列表
  2. 持久单选列表(单选button)
  3. 持久性多项select列表(checkbox)

我将在下面给出一个例子。

传统的单选列表

制作传统的单选列表的方法是使用setItems

在这里输入图像说明

 // setup the alert builder AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Choose an animal"); // add a list String[] 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 dialog AlertDialog dialog = builder.create(); dialog.show(); 

不需要一个OKbutton,因为只要用户点击一个列表项,控件就返回到OnClickListener

单选button列表

在这里输入图像说明

与传统列表相比,单选button列表的优点是用户可以看到当前设置是什么。 制作单选button列表的方法是使用setSingleChoiceItems

 // setup the alert builder AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Choose an animal"); // add a radio button list String[] animals = {"horse", "cow", "camel", "sheep", "goat"}; int checkedItem = 1; // cow builder.setSingleChoiceItems(animals, checkedItem, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // user checked an item } }); // add OK and Cancel buttons builder.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 dialog AlertDialog dialog = builder.create(); dialog.show(); 

我硬编码在这里select的项目,但你可以跟踪一个真正的项目中的类成员variables。

checkbox列表

在这里输入图像说明

制作checkbox列表的方法是使用setMultiChoiceItems

 // setup the alert builder AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Choose some animals"); // add a checkbox list String[] 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 buttons builder.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 dialog AlertDialog dialog = builder.create(); dialog.show(); 

在这里,我硬编码列表中的哪些项目已经被检查。 更有可能的是你想跟踪他们在一个ArrayList<Integer> 。 请参阅文档示例了解更多详细信息。 如果您始终希望所有内容都未选中,您也可以将选中项目设置为null

笔记

  • 您还可以使用setAdaptersetCursor从数据库或其他来源填充​​列表项,或者将CursorListAdapter传入setSingleChoiceItemssetMultiChoiceItems
  • 如果列表长度超过屏幕上显示的长度,对话框会自动滚动。 如果你有一个很长的列表,但我猜你应该用一个RecyclerView做一个自定义的对话框 。
  • 为了testing上面所有的例子,我只用了一个简单的项目,而不是单击button时显示的对话框:

     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 } } 

有关

  • 带有一个,两个和三个button的Android警报对话框
  • 如何实现自定义AlertDialog视图
 final CharSequence[] items = {"A", "B", "C"}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Make your selection"); builder.setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { // Do something with the selection mDoneButton.setText(items[item]); } }); AlertDialog alert = builder.create(); alert.show(); 

使用“ import android.app.AlertDialog; ”导入,然后你写

  String[] items = {"...","...."}; AlertDialog.Builder build = new AlertDialog.Builder(context); build.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //do stuff.... } }).create().show(); 

作为初学者,我build议你通过http://www.mkyong.com/android/android-custom-dialog-example/

我会破解它基本上做了什么

  1. 为对话框和主Activity创build一个XML文件
  2. 在所需位置的主要活动中创build一个android类对象的对象
  3. 基于XML文件添加自定义样式和文本
  4. 调用dialog.show()方法。

这太简单了

 final CharSequence[] items = {"Take Photo", "Choose from Library", "Cancel"}; AlertDialog.Builder builder = new AlertDialog.Builder(MyProfile.this); builder.setTitle("Add Photo!"); builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int item) { if (items[item].equals("Take Photo")) { getCapturesProfilePicFromCamera(); } else if (items[item].equals("Choose from Library")) { getProfilePicFromGallery(); } else if (items[item].equals("Cancel")) { dialog.dismiss(); } } }); builder.show(); 

在AlertDialog中创buildEditText单元之后调用一个方法是否更顺利?

 public static void EditTextListPicker(final Activity activity, final EditText EditTextItem, final String SelectTitle, final String[] SelectList) { EditTextItem.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle(SelectTitle); builder.setItems(SelectList, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialogInterface, int item) { EditTextItem.setText(SelectList[item]); } }); builder.create().show(); return false; } }); }