不同视图中具有相同ID的两个按钮

时间:2019-11-08 11:08:00

标签: java android layout

我正在尝试通过将一个视图设置为不可见而将另一个视图设置为可见来换出片段中的布局。但是,我想在两种情况下都使用相同的Button,在App的UI端工作正常,但是我无法从第二个View的按钮中获取Fragment中的onClick-Event。

这是我的XML:

<?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"
android:id="@+id/id_subfragment"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/id_tv"
    android:gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:textSize="20sp"
    android:text="SomeText" />

<RelativeLayout
    android:id="@+id/id_relLayout_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/id_tv">

    <Button
        android:id="@+id/id_button_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        app:backgroundTint="@color/colorPrimaryDark"
        android:text="Button" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/id_relLayout_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/id_tv">

    <Button
        android:id="@id/id_button_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        app:backgroundTint="@color/colorPrimaryDark"
        android:text="Button" />
 </RelativeLayout>
</RelativeLayout>

如您所见,按钮具有相同的ID,因此,在我的Fragment类(实现View.onClickListener的情况下,我应该能够为它们两个都获取onClick-Event。 片段在onCreateView中的代码:

Button button = v.findViewById(R.id.id_button_next);
button.setOnClickListener(this);

我通过“更改”布局

v.findViewById(R.id.id_relLayout_1).setVisibility(View.GONE);
v.findViewById(R.id.id_relLayout_2).setVisibility(View.VISIBLE);

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

findViewById返回具有给定ID的第一视图,但是如果我没记错的话,您应该能够使用其父母引用这些按钮。 所以代替:

Button button = v.findViewById(R.id.id_button_next);
button.setOnClickListener(this);

您可以使用父容器:

RelativeLayout parent1 = v.findViewById(R.id.id_relLayout_1)
Button buttonInParent1 = parent1.findViewById(R.id.id_button_next)
buttonInParent1.setOnClickListener(this);

RelativeLayout parent2 = v.findViewById(R.id.id_relLayout_2)
Button buttonInParent2 = parent2.findViewById(R.id.id_button_next)
buttonInParent2.setOnClickListener(this);

但是我想提一下,在一个视图中具有相同的ID被认为是一种不好的做法。