自定义列表对话框

柔光的暖阳◎ 2023-10-10 07:56 164阅读 0赞

main.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:gravity="center_horizontal"
  6. android:orientation="vertical" >
  7. <EditText
  8. android:id="@+id/show"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:editable="false"
  12. android:textSize="11pt" />
  13. <Button
  14. android:id="@+id/bn"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="请选择您最擅长的种族" />
  18. </LinearLayout>

row.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="horizontal" >
  6. <!-- 定义一个ImageView,用于作为列表项的一部分。 -->
  7. <ImageView
  8. android:id="@+id/header"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:paddingLeft="10dp" />
  12. <!-- 定义一个TextView,用于作为列表项的一部分。 -->
  13. <TextView
  14. android:id="@+id/name"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:gravity="center_vertical"
  18. android:paddingLeft="10dp"
  19. android:textColor="#ff000066"
  20. android:textSize="16dp" />
  21. </LinearLayout>

java文件

  1. public class CustomListDialog extends Activity {
  2. final int LIST_DIALOG = 0x113;
  3. // 定义3个列表项的名称
  4. private String[] names = new String[] { "神族", "虫族", "人族" };
  5. // 定义3个列表项对应的图标
  6. private int[] imageIds = new int[] { R.drawable.p, R.drawable.z,
  7. R.drawable.t };
  8. @Override
  9. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.main);
  12. Button bn = (Button) findViewById(R.id.bn);
  13. // 为按钮绑定事件监听器
  14. bn.setOnClickListener(new View.OnClickListener() {
  15. @Override
  16. public void onClick(View source) {
  17. // 显示对话框
  18. showDialog(LIST_DIALOG);
  19. }
  20. });
  21. }
  22. // 重写onCreateDialog方法创建对话框
  23. @Override
  24. public Dialog onCreateDialog(int id, Bundle state) {
  25. // 判断需要生成哪种类型的对话框
  26. switch (id) {
  27. case LIST_DIALOG:
  28. Builder b = new AlertDialog.Builder(this);
  29. // 设置对话框的图标
  30. b.setIcon(R.drawable.tools);
  31. // 设置对话框的标题
  32. b.setTitle("单选列表对话框");
  33. // 创建一个List对象,List对象的元素是Map
  34. List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
  35. for (int i = 0; i < names.length; i++) {
  36. Map<String, Object> listItem = new HashMap<String, Object>();
  37. listItem.put("header", imageIds[i]);
  38. listItem.put("personName", names[i]);
  39. listItems.add(listItem);
  40. }
  41. // 创建一个SimpleAdapter
  42. SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems,
  43. R.layout.row, new String[] { "personName", "header" },
  44. new int[] { R.id.name, R.id.header });
  45. // 为对话框设置多个列表
  46. b.setAdapter(simpleAdapter
  47. // 为列表项的单击事件设置监听器
  48. , new DialogInterface.OnClickListener() {
  49. @Override
  50. public void onClick(DialogInterface dialog, int which) {
  51. TextView show = (TextView) findViewById(R.id.show);
  52. // which代表哪个列表项被单击了
  53. show.setText("您最擅长的种族为:" + names[which]);
  54. }
  55. });
  56. // 创建对话框
  57. return b.create();
  58. }
  59. return null;
  60. }
  61. }

效果图
25165647-ad2625996a404f5f89137dc8f7a28823.png

转载于:https://www.cnblogs.com/android-zcq/p/3280984.html

发表评论

表情:
评论列表 (有 0 条评论,164人围观)

还没有评论,来说两句吧...

相关阅读

    相关 定义对话框详解

    不鸡汤上干货 本篇博客内容:讲解如何自定义对话框,并且介绍如何使用(纯手敲,会有点小错,自己改下吧),本功能也可以适用于不同的窗体之间传递参数 可解决问题:系统默认对话框的

    相关 十七、定义进度对话框

    当用户进行需要等待一段时间的操作的时候,我们需要给用户一个等待的反馈,不然用户会以为你的APP卡死了,一般会用一个圆圈来转,但是系统提供的圆圈实在是太丑,所以我自定义一个进度对