如何以编程方式删除添加的视图

时间:2019-02-12 16:47:13

标签: java android view dialog

我有一个对话框,用户可以在其中添加EditText并删除它。我已经以编程方式成功添加了EditText,但是删除它的代码无法正常工作。我正在关注this教程,但就我而言,设置是在对话框中。

我也想获取那些EditText上的所有文本并将其存储在Array中。

这是我的代码:

 public void showSurveyDialog(Context context) {
        ImageButton btnAddChoices, btnRemoveChoice;
        Dialog dialog = new Dialog(context);

        dialog.setContentView(R.layout.survey_content);
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        btnAddChoices = dialog.findViewById(R.id.btn_add_choices);
        LinearLayout choiceLayout = dialog.findViewById(R.id.choice_layout);


        btnAddChoices.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View rowView = dialog.getLayoutInflater().inflate(R.layout.choice_item, null);
                // Add the new row before the add field button.
                choiceLayout.addView(rowView, choiceLayout.getChildCount() - 1);
                ImageButton imageButton = dialog.findViewById(R.id.btn_choice_close);
                imageButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.e("asdass","ASDASd");
                        choiceLayout.removeView((View)v.getParent());
                    }
                });
            }
        });

        dialog.show();

    }

按btnAdd时,选择带有EditText和Button(用于删除)的布局会自动添加到线性布局中。我正在尝试使“删除”按钮起作用,但它不会删除视图。

1 个答案:

答案 0 :(得分:0)

您可以尝试添加和删除引用代码,这可以通过编程方式添加或删除视图:

private void addressField(View view){
        final int[] textCount = {0};
        LinearLayout addressLayout = view.findViewById(R.id.address_layout);
        addressScroll.removeView(addressLayout);
        View layout2 = LayoutInflater.from(view.getContext()).inflate(R.layout.address_text_field
                , addressLayout,false);
        layout2.setTag("address"+Integer.toString(counter));
        addressLayout.addView(layout2);
        addressScroll.addView(addressLayout);
        ImageView deleteButton = layout2.findViewWithTag("address"+Integer.toString(counter))
                .findViewById(R.id.delete_address);
        TextFieldBoxes addressText = layout2.findViewWithTag("address"+Integer.toString(counter))
                .findViewById(R.id.address_wrapper);
        addressText.setSimpleTextChangeWatcher((theNewText, isError) -> {
            if(Objects.equals(theNewText, "")){
                textCount[0] = 0;
                addressLayout.removeView(layout2);
            }

            if(theNewText.length() == 1 && textCount[0] == 0){
                addressField(view);
                ExtendedEditText addressText2 = layout2.findViewWithTag("address"+Integer.toString(counter-1))
                        .findViewById(R.id.address_text);
                addressText2.requestFocus();
                counter++;
            }

            textCount[0]++;
        });
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addressLayout.removeView(layout2);
            }
        });
    }

希望可以解决您的问题