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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章