如何动态地为视图中的每个项目设置 id?

How to set id for each item in a view dynamically?

我是 android 的新手,我试图学习如何制作一个添加按钮,该按钮在单击时动态地在特定视图中添加视图。但是我遇到了不确定如何为要插入到另一个视图的布局中的每个项目设置 id 的问题。 这是 main_view:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/container_layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

<Button
    android:id="@+id/add_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/container_layout"
    android:text="add"
    />



</RelativeLayout>

这是我的代码:

public class MainActivity extends AppCompatActivity {
public int index_num;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button add_button = findViewById(R.id.add_button);
    final LayoutInflater layoutInflater = getLayoutInflater();
    final ViewGroup insertPoint = findViewById(R.id.container_layout);
    index_num = 0;

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

            View new_view = layoutInflater.inflate(R.layout.new_layout,insertPoint, false);
            insertPoint.addView(new_view);
            index_num++;
        }
    });
  }
}

这是我要插入主视图的布局,其中包括 3 个 Edittext:

<?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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:inputType="number"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:inputType="number"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:inputType="number"/>
</LinearLayout>

有人可以教我如何在我按下添加按钮时为三个编辑文本设置不同的 ID 吗?

你可以使用这些方式

使用视图

view.setId(View.generateViewId());

使用 ViewCompat

ViewCompat.generateViewId()

使用约束布局

ConstraintLayout.generateViewId()

正如@Rasoul Miri所说,你可以使用View.generateViewId()为新插入的视图设置id,你应该使用一个列表来记录它们。你还应该为这三个项目设置 id。

像这样:

对于插入的视图

<EditText
    android:id="@+id/one_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:inputType="number"/>
<EditText
    android:id="@+id/two_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:inputType="number"/>
<EditText
    android:id="@+id/three_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:inputType="number"/>

对于 MainActivity

public ArrayList<InsertedView> list;

public void addView() {
    View new_view = layoutInflater.inflate(R.layout.new_layout,insertPoint, false);
    insertPoint.addView(new_view);
    index_num++;
    InsertedView insertedView = new InsertedView(View.generateViewId(), View.generateViewId(), View.generateViewId());
    new_view.findViewById(R.id.one_view).setId(insertedView.firstId);
    new_view.findViewById(R.id.two_view).setId(insertedView.secondId);
    new_view.findViewById(R.id.three_view).setId(insertedView.thirdId);
    list.add(insertedView);
}

class InsertedView {
    public int firstId;
    public int secondId;
    public int thirdId;
    public InsertedView(int one, int two, int three) {
        firstId = one;
        secondId = two;
        thirdId = three;
    }
}