Android 显示一个单选列表对话框(还可利用二维数组配对数据)

本文详细介绍了在Android中使用AlertDialog.Builder设置单选项的方法,包括不考虑item和value配对的简单情况,以及利用二维数组处理item和value配对的更复杂情况。此外,还提供了四种不同的setSingleChoiceItems方法的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、先丢代码,方便“面向搜索引擎编程”的同志们
  1. 不考虑 itemvalue 的配对的情况下(很简单的啦):
    new AlertDialog.Builder(context)
      .setTitle(R.string.title)
      .setSingleChoiceItems(
          R.array.items,    // 显示的每行的内容
          checkedItem,      // 已选中的项( -1 均不选中)
          (dialog, which) -> {
              checkedItem = which;  // 保存选中的项的在 R.array.items 中所处的位置(从 0 开始)
              dialog.dismiss();
          }
      )
      .setNegativeButton(android.R.string.cancel, null)  // “取消”按钮
      .show();
    
  2. itemvalue 配对的情况下(实现中利用了一个二维数组,或许还有更好的方案):
    // 此例中 Items 和 Values 的内容是按顺序一一对应的
    String[][] keyValuePair = {
        getResources().getStringArray(R.array.items),  // Items
        getResources().getStringArray(R.array.values)  // Values
    };
    
    new AlertDialog.Builder(context)
        .setTitle(R.string.title)
        .setSingleChoiceItems(
            keyValuePair[0],              // 显示的每行的内容
            checkedKeyValuePairPosition,  // 已选中的项的位置( -1 均不选中)
            (dialog, which) -> {
                checkedKeyValuePairPosition = which;  // 保存选中的项所处的位置(从 0 开始)
                checkedKeyValuePairValue =
                    keyValuePair[1][which];           // 保存选中的项对应的 Value
                dialog.dismiss();
            }
        )
        .setNegativeButton(R.string.cancel, null)     // “取消”按钮
        .show();
    
二、builder 里提供的 setSingleChoiceItems 目前看是有四种,可以按需使用。上方的代码只用了其中一种,使用方式区别不大,靠官方文档的注释应该就足够了。这里略微带过几小段:

        /**
         * Set a list of items to be displayed in the dialog as the content, you will be notified of
         * the selected item via the supplied listener. This should be an array type i.e.
         * R.array.foo The list will have a check mark displayed to the right of the text for the
         * checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a
         * button will dismiss the dialog.
         *
         * @param itemsId the resource id of an array i.e. R.array.foo
         * @param checkedItem specifies which item is checked. If -1 no items are checked.
         * @param listener notified when an item on the list is clicked. The dialog will not be
         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
         *
         * @return This Builder object to allow for chaining of calls to set methods
         */
        public Builder setSingleChoiceItems(@ArrayRes int itemsId, int checkedItem,
                final OnClickListener listener)
 
        /**
         * Set a list of items to be displayed in the dialog as the content, you will be notified of
         * the selected item via the supplied listener. The list will have a check mark displayed to
         * the right of the text for the checked item. Clicking on an item in the list will not
         * dismiss the dialog. Clicking on a button will dismiss the dialog.
         *
         * @param cursor the cursor to retrieve the items from.
         * @param checkedItem specifies which item is checked. If -1 no items are checked.
         * @param labelColumn The column name on the cursor containing the string to display in the
         *        label.
         * @param listener notified when an item on the list is clicked. The dialog will not be
         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
         *
         * @return This Builder object to allow for chaining of calls to set methods
         */
        public Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn,
                final OnClickListener listener)

        /**
         * Set a list of items to be displayed in the dialog as the content, you will be notified of
         * the selected item via the supplied listener. The list will have a check mark displayed to
         * the right of the text for the checked item. Clicking on an item in the list will not
         * dismiss the dialog. Clicking on a button will dismiss the dialog.
         *
         * @param items the items to be displayed.
         * @param checkedItem specifies which item is checked. If -1 no items are checked.
         * @param listener notified when an item on the list is clicked. The dialog will not be
         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
         *
         * @return This Builder object to allow for chaining of calls to set methods
         */
        public Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener) 

        /**
         * Set a list of items to be displayed in the dialog as the content, you will be notified of
         * the selected item via the supplied listener. The list will have a check mark displayed to
         * the right of the text for the checked item. Clicking on an item in the list will not
         * dismiss the dialog. Clicking on a button will dismiss the dialog.
         *
         * @param adapter The {@link ListAdapter} to supply the list of items
         * @param checkedItem specifies which item is checked. If -1 no items are checked.
         * @param listener notified when an item on the list is clicked. The dialog will not be
         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
         *
         * @return This Builder object to allow for chaining of calls to set methods
         */
        public Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, final OnClickListener listener) 
三、更多

如果需要多选的话,setMultiChoiceItems 拿来用就行了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值