带多选listView的对话框

一:布局文件

<?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=".Activity6"
    android:padding="20dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="带多选listView的对话框"
        android:textSize="23sp"
        android:gravity="center"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="名单:"/>
        <EditText
            android:id="@+id/edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_border"
            android:focusable="false"/>
    </LinearLayout>

    <Button
        android:id="@+id/btn_submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提交"
        android:layout_above="@+id/btn"/>
    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="返回"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

二:调用

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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 Activity6 extends AppCompatActivity {
    // 选择人员的编号
    String ids = "";

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

        initView();
    }

    private void initView() {
        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        /**
         * 模拟list数据
         */
        List<Map<String, String>> list = new ArrayList<>();
        Map map1 = new HashMap();
        map1.put("name", "张三");
        map1.put("age", "18");
        map1.put("gender", "男");
        map1.put("number", "001");
        list.add(map1);
        Map map2 = new HashMap();
        map2.put("name", "翠花");
        map2.put("age", "16");
        map2.put("gender", "女");
        map2.put("number", "002");
        list.add(map2);
        Map map3 = new HashMap();
        map3.put("name", "李四");
        map3.put("age", "20");
        map3.put("gender", "男");
        map3.put("number", "003");
        list.add(map3);
        Map map4 = new HashMap();
        map4.put("name", "熊大");
        map4.put("age", "32");
        map4.put("gender", "男");
        map4.put("number", "004");
        list.add(map4);
        
        EditText edit = findViewById(R.id.edit);
        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean[] selected = new boolean[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    selected[i] = false;
                }
                String[] name = new String[list.size()];
                String[] number = new String[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    name[i] = list.get(i).get("name");
                    number[i] = list.get(i).get("number");
                }

                AlertDialog.Builder builder = new AlertDialog.Builder(Activity6.this);
                builder.setTitle("参赛名单");
                DialogInterface.OnMultiChoiceClickListener multiChoiceClickListener = new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        selected[which] = isChecked;
                    }
                };
                builder.setMultiChoiceItems(name, selected, multiChoiceClickListener);
                DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String names = "";
                        String numbers = "";
                        for (int i = 0; i < selected.length; i++) {
                            if (selected[i] == true) {
                                if (names.equals("")) {
                                    names = names + name[i];
                                    numbers = numbers + number[i];
                                } else {
                                    names = names + "," + name[i];
                                    numbers = numbers + "," + number[i];
                                }
                            }
                        }
                        edit.setText(names);
                        ids = numbers;
                    }
                };
                builder.setNegativeButton("取消", null);
                builder.setPositiveButton("确定", listener);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });

        Button btn_submit = findViewById(R.id.btn_submit);
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Activity6.this, "参赛名单:" + edit.getText().toString().trim() + "  编号:" + ids, Toast.LENGTH_LONG).show();
            }
        });
    }
}

三:效果

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Qxnedy

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

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

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

打赏作者

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

抵扣说明:

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

余额充值