简体   繁体   中英

Two Buttons with same ID in different Views

I'm trying to swap out Layouts in my Fragment, by setting one View invisivle and the other one visible. However I want to use the same Button in both cases, which works fine on the UI-Side of the App, but I can't get the onClick-Event in the Fragment from the button in the second View.

Here is my 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>

As you see, the buttons have the same ID, so in my Fragment-class (which implements View.onClickListener I should be able to get the onClick-Event for both of them. Code from Fragment in onCreateView:

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

I 'change' the layouts with

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

Thanks for any help!

findViewById returns first view with the given ID, but if I'm not wrong you should be able to reference those buttons using their parents. So instead of:

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

you could use parent containers:

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

But I want mention that having the same ids within a single view is considered as a bad practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM