一:布局文件
<?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();
}
});
}
}
三:效果