Как удалить добавленные виды программно - PullRequest
0 голосов
/ 12 февраля 2019

У меня есть диалог, в котором пользователь может добавить EditText, а также удалить его.Я успешно добавил EditText программно, но мой код для его удаления не работает.Я следую этому учебнику, но в моем случае настройка находится внутри диалога.

, а также я хочу получить все тексты этих EditTexts и сохранить их в массиве.

Это мой код:

 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();

    }

При нажатии btnAddChoices макет с EditText и Button (для удаления) автоматически добавляется в линейный макет.Я пытаюсь заставить кнопку удаления работать, но она не удаляет вид.

1 Ответ

0 голосов
/ 13 февраля 2019

Вы можете попробовать мой код добавления и удаления для ссылок, это может добавить или удалить представление программно:

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);
            }
        });
    }

надеюсь, может решить ваши проблемы

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...