Android - 与TextView和ImageView相同的OnClickListener

use*_*259 3 android onclicklistener

我有3个项目的列表:

[image_1] [text_1]
[image_2] [text_2]
[image_3] [text_3]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我没有使用ListView.相反,我使用了RelativeLayout.

我如何处理TextView和ImageView的OnClickListener?

list_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

<ImageView
    android:id="@+id/image_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="21dp"
    android:layout_marginTop="21dp"
    android:src="@drawable/calbutton" />

<TextView
    android:id="@+id/text_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/image_1"
    android:layout_toRightOf="@+id/image_1"
    android:text="TextView" />

   <ImageView
    android:id="@+id/image_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBelow="@+id/image_1"
    android:layout_below="@+id/image_1"
    android:layout_marginLeft="21dp"
    android:layout_marginTop="21dp"
    android:src="@drawable/calbutton" />

   <TextView
     android:id="@+id/text_2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignLeft="@+id/text_1"
     android:layout_alignTop="@+id/image_2"
     android:text="TextView" />

   <ImageView
    android:id="@+id/image_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBelow="@+id/image_2"
    android:layout_below="@+id/image_2"
    android:layout_marginLeft="21dp"
    android:layout_marginTop="21dp"
    android:src="@drawable/calbutton" />  

   <TextView
       android:id="@+id/text_3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignTop="@+id/image_3"
       android:layout_toRightOf="@+id/image_3"
       android:text="TextView" />

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

KDe*_*kar 5

您可以为该视图添加一个父视图,并向该父视图调用单击侦听器,就像您可以将LinearLayout添加为父视图一样:

<LinearLayout android:id="@+id/firstparent">
<Imageview/>
<TextView/>
</LinearLayout>
<LinearLayout android:id="@+id/secondparent">
<Imageview/>
<TextView/>
</LinearLayout>
<LinearLayout android:id="@+id/thirdparent">
<Imageview/>
<TextView/>
</LinearLayout>
<LinearLayout android:id="@+id/forthparent">
<Imageview/>
<TextView/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

现在设置单击侦听器,LinearLayout以便无论单击imageview还是textview,都可以获得相同的事件.

然后为每个布局注册onClickListner.

layout1.setOnClickListener(this);
layout2.setOnClickListener(this);
layout3.setOnClickListener(this);
layout4.setOnClickListener(this);

@Override
public void onClick ( View v )
{
     if ( v == layout1 ) 
     {
            // your code...
     }
     else if(v == layout2){
         // your code...
     }
   ////  add others...
}
Run Code Online (Sandbox Code Playgroud)