Android 是否无法使用addView()动态添加自定义布局?

Android 是否无法使用addView()动态添加自定义布局?,android,dialogfragment,Android,Dialogfragment,我正在制作一个窗口来打开一个对话框窗口,并写一条评论 打开对话框窗口时,会出现一个布局,您可以在其中输入第一条注释 注释只能写在一行中,按输入字段中的EnterKey可创建以下注释布局 我最初是作为RecyclerView实现的,但现在我也在尝试其他方法。(使用addView()) 但是当我使用addView()时,我得到了以下错误 java.lang.IllegalStateException: The specified child already has a parent. You mus

我正在制作一个窗口来打开一个
对话框窗口
,并写一条评论

打开对话框窗口时,会出现一个
布局
,您可以在其中输入第一条注释

注释只能写在一行中,按输入字段中的
EnterKey
可创建以下注释布局

我最初是作为RecyclerView实现的,但现在我也在尝试其他方法。(使用
addView()

但是当我使用
addView()
时,我得到了以下错误

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
解决方案如中所示。(如果您能解释发生此错误的原因,我将不胜感激。)

使用此选项时没有错误。 但是,不会添加更多的项目

对话框打开时只有
第一项
,按
回车键时没有响应

我想实时动态添加项目

我该怎么办


fragment\u wc\u dialog.xml


已添加


据我所知,您的问题和相关问题是不同的。链接有两个视图,它们希望在这两个视图之间来回交换,因此答案是在添加第二个视图之前删除一个视图。您希望建立视图列表,因此必须使用不同的实例,而不是反复添加相同的视图

如果希望新添加的视图也响应Enter键,也可以将相同的侦听器设置为新视图:

if(event.getAction() == KeyEvent.ACTION_DOWN) {
    EditText addedView = (EditText) LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, mContainer, false);
    addedView.setOnEditorActionListener(this);
    mContainer.addView(addedView);
}

哇!非常感谢。但这里还有另一个问题。并非在所有创建的视图中都会触发
EnterKey(Add View)
事件,但只有创建的
first View
是项添加事件。是否有任何方法可以为添加的项目触发事件?是否有任何方法可以为添加的项目触发事件?因此,添加的视图是一个新的
EditText
?您希望能够在新的
编辑文本中键入内容,按Enter键,然后添加一个附加视图?没错。这是同一个项目。我希望在添加的每个视图上触发enter键事件,而不仅仅是第一个视图。我上传了一张照片。我想要一个像照片这样的功能。在照片中,所有添加的项目(注释)都会发生enter键事件。但我只为第一件事开火。有没有办法为所有添加的项目添加查看事件?@yb我已经更新了答案谢谢。onEditorAction()是否在外部实现?我以后再试试。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".fragment.WritingCommentDialogFragment">

    <EditText
        android:id="@+id/comment_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:gravity="center_vertical"
        android:drawableLeft="@drawable/ic_bullet_point"
        android:drawablePadding="5dp"
        android:layout_marginHorizontal="10dp"
        android:background="@null"
        android:textSize="15sp"
        android:inputType="text"
        android:maxLines="1"
        android:maxLength="22"
        android:imeOptions="actionNone"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
public class WritingCommentDialogFragment extends DialogFragment {
    LinearLayout mContainer; // input layout
    View inputView;
    EditText editText;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);

        initViews(view);

        mContainer.addView(inputView); // The first item to be present when the dialog is opened

        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(event.getAction() == KeyEvent.ACTION_DOWN) {
                    mContainer.addView(inputView);
                }
                return true;
            }
        });

        return view;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

    @Override
    public void onResume() {
        super.onResume();
        Toast.makeText(getContext(), "onResume()", Toast.LENGTH_SHORT).show();
        setDialogSize();
    }

    private void initViews(View view) {
        mContainer = view.findViewById(R.id.container);
        inputView = LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, null);
        editText = inputView.findViewById(R.id.comment_edit);
    }
}
if(event.getAction() == KeyEvent.ACTION_DOWN) {
    EditText addedView = (EditText) LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, mContainer, false);
    addedView.setOnEditorActionListener(this);
    mContainer.addView(addedView);
}