Android多选下拉框

一:布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activity15"
    android:padding="20dp">

    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:focusable="false"/>

</RelativeLayout>

二:使用

package com.example.test;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Activity15 extends AppCompatActivity {
    private EditText edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_15);

        // 数据源
        List<Map> list = new ArrayList();
        // 模拟数据
        Map map1 = new HashMap();
        map1.put("hobbies", "电影");
        map1.put("name", "张三");
        map1.put("age", 20);
        map1.put("id", 1);
        list.add(map1);
        Map map2 = new HashMap();
        map2.put("hobbies", "游戏");
        map2.put("name", "李四");
        map2.put("age", 18);
        map2.put("id", 2);
        list.add(map2);
        Map map3 = new HashMap();
        map3.put("hobbies", "运动");
        map3.put("name", "王五");
        map3.put("age", 17);
        map3.put("id", 3);
        list.add(map3);
        Map map4 = new HashMap();
        map4.put("hobbies", "音乐");
        map4.put("name", "赵六");
        map4.put("age", 15);
        map4.put("id", 4);
        list.add(map4);
        // 下拉项选中状态
        boolean[] selected = new boolean[list.size()];
        // 下拉框数据源
        String[] str = new String[list.size()];
        // 下拉项id
        int[] id = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            str[i] = list.get(i).get("hobbies").toString();
            id[i] = (int) list.get(i).get("id");
            selected[i] = false;
        }

        edit = findViewById(R.id.edit);
        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkboxEdit(edit, selected, str, id);
            }
        });
    }

    /**
     * 多选下拉框
     * @param edit 下拉框
     * @param selected 下拉项选中状态集合
     * @param str 下拉项数据源
     * @param id 下拉项id
     */
    private void checkboxEdit(EditText edit, boolean[] selected, String[] str, int[] id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("兴趣爱好");
        DialogInterface.OnMultiChoiceClickListener multiChoiceClickListener = new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int i, boolean isChecked) {
                selected[i] = isChecked;
            }
        };
        builder.setMultiChoiceItems(str, selected, multiChoiceClickListener);
        DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String selectStr = "";
                String ids = "";
                for (int i = 0; i < selected.length; i++) {
                    if (selected[i]) {
                        if (TextUtils.isEmpty(selectStr)) {
                            selectStr = selectStr + str[i];
                            ids = ids + id[i];
                        } else {
                            selectStr = selectStr + "," + str[i];
                            ids = ids + "," + id[i];
                        }
                    }
                }
                edit.setText(selectStr);
                Toast.makeText(Activity15.this, ids, Toast.LENGTH_SHORT).show();
            }
        };
        builder.setCancelable(false);
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", clickListener);
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}

三:效果图

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Qxnedy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值