如何使相对布局onclick工作?

bha*_*kar 3 xml android onclicklistener

我的相对布局为:

    <RelativeLayout
        android:id="@+id/iPay_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:orientation="horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="· Use iPay"
            android:textColor="#686A86"
            android:textSize="18sp" />


        <Button
            android:layout_width="50dp"
            android:layout_height="35dp"
            android:layout_alignParentRight="true"
            android:background="@drawable/i_pay"
            android:textAllCaps="false"
            android:textColor="#ffffff" />

    </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我希望我的相对布局执行onCLickListener但不起作用.我能够执行setonclicklistener但它只适用于textview部分而不适用于图像.

    btniPay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //for ipay

        }
    });
Run Code Online (Sandbox Code Playgroud)

这仅适用于文本部分,但不适用于图像.知道我可能会缺少什么.

小智 6

或者,您可以在按钮xml属性中添加android:clickable ="false".

<RelativeLayout
    android:id="@+id/iPay_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:orientation="horizontal">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="· Use iPay"
        android:textColor="#686A86"
        android:textSize="18sp" />


    <Button
        android:layout_width="50dp"
        android:layout_height="35dp"
        android:layout_alignParentRight="true"
        android:background="@drawable/i_pay"
        android:textAllCaps="false"
        android:clickable="false"
        android:textColor="#ffffff" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)