You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android多选选项功能名称及Android Studio实现方法问询

可多选选项功能的名称及Android Studio实现方案

功能名称

这个可同时选择多个选项的功能叫做多选列表(Multiple Selection List),常见实现形式分为两种:嵌入页面的自定义多选列表、弹窗式多选对话框。


一、嵌入页面的自定义多选列表(RecyclerView实现)

适合需要在页面中固定展示的多选场景。

1. 创建列表项布局(res/layout/item_multiple_select.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="16dp"
    android:gravity="center_vertical">

    <CheckBox
        android:id="@+id/item_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"/>

    <TextView
        android:id="@+id/item_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"/>
</LinearLayout>

2. 实现RecyclerView适配器

class MultipleSelectAdapter(
    private val dataList: List<String>,
    private val selectedItems: MutableSet<String>
) : RecyclerView.Adapter<MultipleSelectAdapter.ViewHolder>() {

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val checkbox: CheckBox = itemView.findViewById(R.id.item_checkbox)
        val titleTv: TextView = itemView.findViewById(R.id.item_title)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_multiple_select, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = dataList[position]
        holder.titleTv.text = item
        holder.checkbox.isChecked = selectedItems.contains(item)

        holder.checkbox.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) selectedItems.add(item) else selectedItems.remove(item)
        }
    }

    override fun getItemCount() = dataList.size
}

3. 在Activity中初始化RecyclerView

class MainActivity : AppCompatActivity() {
    private val dataList = listOf("选项1", "选项2", "选项3", "选项4", "选项5")
    private val selectedItems = mutableSetOf<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = MultipleSelectAdapter(dataList, selectedItems)

        // 获取选中项示例
        findViewById<Button>(R.id.btn_get_selected).setOnClickListener {
            val selectedText = selectedItems.joinToString(", ")
            Toast.makeText(this, "选中项:$selectedText", Toast.LENGTH_SHORT).show()
        }
    }
}

二、弹窗式多选对话框(AlertDialog实现)

适合临时触发的多选场景,无需自定义布局,直接使用系统组件。

class MainActivity : AppCompatActivity() {
    private val options = arrayOf("选项1", "选项2", "选项3", "选项4", "选项5")
    private val selectedFlags = booleanArrayOf(false, false, false, false, false)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<Button>(R.id.btn_show_dialog).setOnClickListener {
            AlertDialog.Builder(this)
                .setTitle("请选择选项")
                .setMultiChoiceItems(options, selectedFlags) { _, position, isChecked ->
                    selectedFlags[position] = isChecked
                }
                .setPositiveButton("确定") { _, _ ->
                    val selectedList = mutableListOf<String>()
                    options.forEachIndexed { index, s ->
                        if (selectedFlags[index]) selectedList.add(s)
                    }
                    Toast.makeText(this, "选中项:${selectedList.joinToString(", ")}", Toast.LENGTH_SHORT).show()
                }
                .setNegativeButton("取消", null)
                .show()
        }
    }
}

内容的提问来源于stack exchange,提问作者Manmohan Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 16:38:24