简体   繁体   中英

Android - same OnClickListener for TextView and ImageView

I have a list of 3 items:

[image_1] [text_1]
[image_2] [text_2]
[image_3] [text_3]

在此输入图像描述

I'm not using a ListView. Instead I used a RelativeLayout.

How would I handle an OnClickListener for both the TextView and ImageView?

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>

you can add one parent view for both view and call click listener to that parent view like you can add LinearLayout as parent:

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

Now set the click listener to LinearLayout so you can get the same event whether you click the imageview or textview.

then register the onClickListner for each layout like.

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

Put the imageview and textview inside a linear layout . then override onClickLListener for the LinearLayout

<RelativeLayout ...... >

    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
        android:orientation="vertical" >

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

    </LinearLyaout>

  .......

</RelativeLayout>

对于ListView您可以添加一个onItemClickListener ,将在该行的任何位置单击调用。

Just implements the OnClickListener() to your java file like beflow,

public yourClassName extends Activity implements OnClickListener
{

    ....your code

    // register for onClickListener
    text1.setOnClickListener(this); 
    // register same for other textboxes & imageview.

    @Override
    public void onClick ( View view )
    {
         if ( view == text1 ) // assuming text1 is your text_1 of your .xml file
         {
                // do stuff
         }
         ... 
         // same for other textboxes & image view.
    }
}

just use OnClickListeners for all textviews and imageviews like this

findViewById(R.id.image_1).setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       ////// write your code here
   }
});

If you want one handler with different actions for each View, you could do this:

TextView tv;
ImageView iv;
View.OnClickListener myOnlyhandler = new View.OnClickListener() {
  public void onClick(View v) {
      if( tv.getId() == ((TextView)v).getId() ){
          // it was the textview
      }
      else if(iv.getId() == ((ImageView)v).getId() ){
          // it was the imageview
      }
  }
}

Or if you want to group the two Views together, put them in a container (eg RelativeLayout, LinearLayout, etc) and assign an OnClickListener to the container.

This is the code

TextView tv[] = new TextView[subCategory.length];
    for (int i = 0; i < subCategory.length; i++) {
            tv[i] = new TextView(this);
            tv[i].setText(subCategory[i]);
            tv[i].setId(i);
            sCategoryLayout.addView(tv[i]);
            tv[i].setOnClickListener(onclicklistener);
        }    

onclicklistener method :

OnClickListener onclicklistener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v == tv[0]){
            //do whatever you want....
        }
    }
};

You can either set the onClick event like below for both the image/button/textview/edittext etc and have the method in your Activity.

在此输入图像描述

<Button
    android:id="@+id/some_ui_element"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#00FFFFFF"
    android:onClick="onClickButton"
    android:shadowColor="#000"
    android:text="@string/sometitle"
    android:textColor="@android:color/primary_text_dark_nodisable" />

/**
 * onClickView() called
 * @param v
 */
public void onClickView(View v) {
  //Do something
      switch(v.getId()){
         case R.id.someuielement :
         break;
         ...
         ...
      }
}

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