相对布局 onCLick 更改背景和描边颜色

Relative Layout onCLick change background and stroke color

当我点击相对布局时,背景和描边颜色没有改变。我提到了所有关键字,即 android:state_focused="true"android:state_pressed="true"android:state_selected="true",但什么也没发生。我的代码有什么问题。请帮忙。

相对布局:

<RelativeLayout
                    android:id="@+id/plan2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:background="@drawable/shape_rectangle_plan"
                    android:padding="15dp"
                    android:focusable="true">

                    <TextView
                        android:id="@+id/text1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:fontFamily="@font/roboto_medium"
                        android:text="₹5000"
                        android:textColor="#000000"
                        android:textSize="20sp" />


                </RelativeLayout>

shape_rectangle_plan - XML

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true">
        <shape>
            <size android:height="1dp" />
            <solid android:color="#12FDB559" />
            <stroke android:width="0.8dp" android:color="@color/color2" />
        </shape>
    </item>

    <item>
        <shape>
            <size android:height="1dp" />
            <solid android:color="#12FFFFFF" />
        </shape>
    </item>
</selector>

主要活动:

relative_layout.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View view) {

                relative_layout.setSelected(true);
            }
        });

selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true">
        <shape>
            <size android:height="1dp" />
            <solid android:color="#12FDB559" />
            <stroke android:width="0.8dp" android:color="@color/color2" />
        </shape>
    </item>

    <item
        android:state_selected="false">
        <shape>
            <size android:height="1dp" />
            <solid android:color="#12FFFFFF" />
        </shape>
    </item>
</selector>

在你的class中:

relative_layout.setOnClickListener {
          relative_layout.setSelected(true)
}