0

我正在开发一个关于 Android Studio 的项目。然后我想实现一个在他上方生成视图的按钮。并将其文本数据存储在 ArrayList 中。

这是java代码:

addClassBtn.setOnClickListener((v)->{
        View cricketerView = getLayoutInflater().inflate(R.layout.check_box,categoryList,false);
        EditText checkBox_title = (EditText)cricketerView.findViewById(R.id.checkBox_title);
        switch (appPreference.getInt("lang",0)){
            case 0:checkBox_title.setHint(getResources().getString(R.string.en_en_editBox_Text));break;
            case 1:checkBox_title.setHint(getResources().getString(R.string.ch_tw_editBox_Text));break;
            default:checkBox_title.setHint("Error");
        }
        temporaryClass.add(checkBox_title.getText().toString());
        checkBox_title.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                temporaryClass.remove(checkBox_title.getText().toString());
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                temporaryClass.add(checkBox_title.getText().toString());
            }
        });
        CheckBox checkBox_check = (CheckBox)cricketerView.findViewById(R.id.checkBox_check);
        checkBox_check.setOnClickListener((w)->{
        });
        Button checkBox_delete = (Button)cricketerView.findViewById(R.id.checkBox_delete);
        checkBox_delete.setOnClickListener((w)->{
            categoryList.removeView(w);
        });
        categoryList.addView(cricketerView);
    });

这是我想在按钮上方添加的视图的 XML 布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
    android:id="@+id/checkBox_title"
    android:layout_width="295dp"
    android:layout_height="40dp"
    android:layout_marginStart="13dp"
    android:layout_marginTop="7dp"
    android:layout_marginEnd="5dp"
    android:autofillHints="@string/ch_tw_editBox_Text"
    android:background="@drawable/rounded_edittext"
    android:hint="@string/ch_tw_editBox_Text"
    android:inputType="text"
    android:paddingStart="13dp"
    android:paddingEnd="13dp"
    android:selectAllOnFocus="false"
    android:textSize="22sp" />
<CheckBox
    android:id="@+id/checkBox_check"
    android:layout_width="30dp"
    android:layout_height="40dp"
    android:layout_marginStart="5dp"
    android:layout_marginTop="5dp"
    android:layout_marginEnd="5dp"
    android:text=""
    android:textColorLink="#000000" />
<Button
    android:id="@+id/checkBox_delete"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:background="@drawable/ic_delete"
    android:layout_marginStart="5dp"
    android:layout_marginEnd="13dp"
    android:layout_marginTop="5dp"/>
</LinearLayout>

然后当我尝试它时,它只生成一次视图,我无法生成更多视图。我什至无法删除生成的视图。

测试用例:</p>

在此处输入图像描述

我已经在其他活动中使用了上面的代码,而且我工作得非常好。

谢谢

4

0 回答 0