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