Android 多选下拉框的简单实现

1、布局文件(activity_dropdown.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".DropDownActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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>

2、Activity文件(DropDownActivity)

public class DropDownActivity extends AppCompatActivity {

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

        List<Map> list = new ArrayList();
        Map map1 = new HashMap();
        map1.put("hobbies","电影");
        map1.put("name","张三");
        map1.put("id","1");
        list.add(map1);
        Map map2 = new HashMap();
        map2.put("hobbies","游戏");
        map2.put("name","李四");
        map2.put("id","2");
        list.add(map2);
        Map map3 = new HashMap();
        map3.put("hobbies","运动");
        map3.put("name","王五");
        map3.put("id","3");
        list.add(map3);

        //下拉项选中状态
        final boolean[] selected = new boolean[list.size()];
        //下拉框数据源
        final String[] str = new String[list.size()];
        //下拉项ID
        final 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");
            id[i] = Integer.parseInt(list.get(i).get("id").toString());
            selected[i] = false;
        }
       final EditText edit = findViewById(R.id.edit);
        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkboxEdit(edit,selected,str,id);
            }
        });

    }

    private void checkboxEdit(final EditText edit, final boolean[] selected,final String[] str,final int[] id){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("兴趣爱好");
        DialogInterface.OnMultiChoiceClickListener multiChoiceClickListener = new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                selected[which] = 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(DropDownActivity.this,ids,Toast.LENGTH_SHORT).show();
            }
        };
        builder.setCancelable(false);
        builder.setNegativeButton("取消",null);
        builder.setPositiveButton("确定",clickListener);
        AlertDialog dialog = builder.create();
        dialog.show();
    }

}

3、运行效果

 

 参考网址:

https://blog.csdn.net/Qxnedy/article/details/127637451

posted @ 2023-09-25 14:20  小小邪  阅读(689)  评论(0编辑  收藏  举报