2013-03-12 49 views
3

相同OnClickListener我有3個項目的列表:Android的 - 爲的TextView和ImageView的

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

enter image description here

我不使用一個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> 
+3

我不明白確切的問題。如果您只需爲ImageView和TextView添加相同的'OnclickListener',則可以通過將這兩個視圖添加到單個父視圖('Linearlayout')併爲該父視圖添加'OnClickListener'來完成此操作。 – sujith 2013-03-12 04:34:00

回答

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> 

現在設置的點擊聽衆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... 
} 
+0

太棒了!你能讓我開始瞭解OnClickListener的每一個? – user1040259 2013-03-12 04:41:52

+0

我編輯了我的答案 – KDeogharkar 2013-03-12 04:54:33

+0

完全明白了。謝謝! – user1040259 2013-03-12 04:57:06

1

就實現了OnClickListener()像beflow你的java文件,

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

只使用OnClickListeners所有textviews和imageviews這樣

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

imageviewtextviewlinear layout 。然後覆蓋onClickLListenerLinearLayout

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

如果你想爲每個視圖不同的操作一個處理程序,你可以這樣做:

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

或者,如果你想組兩個視圖一起,把他們在容器中(例如RelativeLayout,LinearLayout等),並將OnClickListener分配給容器。

0

這是代碼

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方法:

OnClickListener onclicklistener = new OnClickListener() { 

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

您可以爲圖片/按鈕/ textview/edittext等設置下面的onClick事件,並在您的活動中使用該方法。

enter image description here

<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; 
     ... 
     ... 
     } 
} 
相關問題