如何使相对布局onclick工作?

时间:2016-05-03 09:42:21

标签: android xml 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>

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

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

            //for ipay

        }
    });

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

4 个答案:

答案 0 :(得分:5)

或者,您可以在按钮xml属性中添加 android:clickable =&#34; false&#34;

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

答案 1 :(得分:1)

可能你可以找到一些与imageview和按钮一起使用的好例子,但我通过使用添加TextView而不是图像和按钮来实现它。

<RelativeLayout
            android:id="@+id/layout1"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:clickable="true"
            android:gravity="right">

            <TextView
                android:id="@+id/englishheading1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_alignParentLeft="true"
                android:layout_toLeftOf="@+id/urduheading1"
                android:text="English text"
                android:textColor="#ffffff"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/urduheading1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="15dp"
                android:text="urdu text1"
                android:textColor="#ffffff"
                android:textSize="16sp" />

            <View
                android:layout_width="match_parent"
                android:layout_height="4dp"
                android:layout_alignParentBottom="true"
                android:layout_marginTop="8dp"
                android:background="#ffffff" />
        </RelativeLayout>

以下是执行onClick的java代码。

RelativeLayout layout1 = (RelativeLayout) view.findViewById(R.id.layout1);
    layout1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // do something
        }
});

答案 2 :(得分:0)

看到我的代码轻松运行更改您的代码:

.xml文件

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="${relativePackage}.${activityClass}" >

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

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

            <Button
                android:id="@+id/btn"
                android:layout_width="50dp"
                android:layout_height="35dp"
                android:layout_alignParentRight="true"
                android:background="@drawable/ic_launcher"
                android:textAllCaps="false"
                android:textColor="#ffffff" />
        </RelativeLayout>

    </RelativeLayout>

.java文件

        public class MainActivity extends Activity {

        TextView tv;
        Button btn;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            tv=(TextView) findViewById(R.id.tv);
            btn = (Button) findViewById(R.id.btn);

            tv.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();
                }
            });
            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "button", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

答案 3 :(得分:0)

只需在android:clickable="true"

中标记RelativeLayout即可
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_id"
    android:clickable="true">

使用 ButterKnife lib make bind here:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xml_name);

        ButterKnife.bind(this);
    }

@OnClick(R.id.activity_id)
void onActivityClick() {
      // do smth ...
}