复选框,可启用Android编辑框

时间:2019-04-04 15:38:26

标签: android android-edittext android-dialog android-checkbox

我需要创建餐厅类型列表。我已经编写了一个简单的警报对话框,其中显示了一系列复选框。但是我希望最后一个叫做“更多”的按钮能够启用一个编辑框,用户可以在其中编辑自己的自定义类型,但我不知道该怎么做。我是初学者。

这是我的应用程序的当前输出:

App Output

我想成为这样:

Wanted output

如果需要,这里有一些对话框代码:

private TextView text; private String[] listItems; private boolean[] checkedItems; private ArrayList<Integer> selectedItems = new ArrayList<>();

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

    text = findViewById(R.id.textView);

    listItems = getResources().getStringArray(R.array.menu);
    checkedItems = new boolean[listItems.length];

    text.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Type of cusine");
            builder.setMultiChoiceItems(listItems, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    if(isChecked){
                        if(!selectedItems.contains(which)){

                            if(which == listItems.length-1){
                                dialog.dismiss();

                                AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this);
                                builder2.setTitle("Enter your type");

                            }

                            selectedItems.add(which);
                        }
                    }else if(selectedItems.contains(which)){
                        selectedItems.remove(which);            //To avoid duplicates
                    }
                }
            });

            builder.setCancelable(false);
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String item = "";
                    for(int i=0; i<selectedItems.size(); i++){
                        item = item + listItems[selectedItems.get(i)];
                        if(i != selectedItems.size() - 1){
                            item = item + ", ";
                        }
                    }
                    text.setText(item);
                }
            });

            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

            builder.setNeutralButton("Clear all", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    for(int i=0; i<checkedItems.length; i++){
                        checkedItems[i] = false;
                        selectedItems.clear();
                        text.setText("");
                    }
                }
            });

            AlertDialog dialog = builder.create();
            dialog.show();
        }
    });
}

0 个答案:

没有答案