2011-09-09 173 views
0

我有以下XML佈局:與setOnClickListener()有麻煩

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:background="#ffffff" android:layout_height="fill_parent"> 
    <LinearLayout android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:orientation="vertical" 
     android:layout_gravity="center"> 
     <TextView android:text="Title1" android:id="@+id/title1" 
      android:padding="5dip" android:background="#005555" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
     <LinearLayout android:id="@+id/panel1" 
      android:visibility="gone" android:orientation="vertical" 
      android:layout_width="fill_parent" android:layout_height="wrap_content"> 
      <TextView android:layout_margin="2dip" android:text="Item1" 
       android:padding="5dip" android:background="#777777" 
       android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
      <TextView android:layout_margin="2dip" android:text="Item2" 
       android:padding="5dip" android:background="#777777" 
       android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
      <TextView android:layout_margin="2dip" android:text="Item3" 
       android:padding="5dip" android:background="#777777" 
       android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
     </LinearLayout> 
    </LinearLayout> 
    <LinearLayout android:layout_marginTop="2dip" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:orientation="vertical" android:layout_gravity="center"> 
     <TextView android:text="Title2" android:id="@+id/title2" 
      android:padding="5dip" android:background="#005555" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
     <LinearLayout android:id="@+id/panel2" 
      android:visibility="gone" android:orientation="vertical" 
      android:layout_width="fill_parent" android:layout_height="wrap_content"> 
      <TextView android:layout_margin="2dip" android:text="Item1" 
       android:padding="5dip" android:background="#777777" 
       android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
      <TextView android:layout_margin="2dip" android:text="Item2" 
       android:padding="5dip" android:background="#777777" 
       android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
      <TextView android:layout_margin="2dip" android:text="Item3" 
       android:padding="5dip" android:background="#777777" 
       android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
     </LinearLayout> 
    </LinearLayout> 
    <LinearLayout android:layout_marginTop="2dip" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:orientation="vertical" android:layout_gravity="center"> 
     <TextView android:text="Title3" android:id="@+id/title3" 
      android:padding="5dip" android:background="#005555" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
     <LinearLayout android:id="@+id/panel3" 
      android:visibility="gone" android:orientation="vertical" 
      android:layout_width="fill_parent" android:layout_height="wrap_content"> 
      <TextView android:layout_margin="2dip" android:text="Item1" 
       android:padding="5dip" android:background="#777777" 
       android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
      <TextView android:layout_margin="2dip" android:text="Item2" 
       android:padding="5dip" android:background="#777777" 
       android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
      <TextView android:layout_margin="2dip" android:text="Item3" 
       android:padding="5dip" android:background="#777777" 
       android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

而且我想設置TextViews「TITLE1」,「標題2」和「TITLE3」點擊收聽,通過雖然循環並找到相應的兒童觀:

import java.util.ArrayList; 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class MyActivity extends Activity implements View.OnClickListener { 
    private static final String TAG = "MyActivity"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mPanelWrappers = new ArrayList<LinearLayout>(); 
     LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     ViewGroup rootLayout = (ViewGroup)inflater.inflate(R.layout.main, null); 
     for(int i = 0; i < rootLayout.getChildCount(); i++) { 
      View rootChild = rootLayout.getChildAt(i); 
      if(rootChild instanceof LinearLayout) { 
       LinearLayout panelWrapper = (LinearLayout)rootChild; 
       mPanelWrappers.add(panelWrapper); 
       for(int j = 0; j < panelWrapper.getChildCount(); j++) { 
        View wrapperChild = panelWrapper.getChildAt(j); 
        if(wrapperChild instanceof TextView) { 
         Log.d(TAG, "Setting on-click listener for " + wrapperChild.getId()); 
         if(wrapperChild == findViewById(R.id.title1)) { 
          Log.d(TAG, "Found title1"); 
         } else { 
          Log.d(TAG, "Not title1"); 
         } 
         wrapperChild.setOnClickListener(this); 
        } 
        break; 
       } 
      } 
     } 
    } 

    @Override 
    public void onClick(View panelTitle) { 
     Log.d(TAG, "On click"); 
    } 

    private ArrayList<LinearLayout> mPanelWrappers; 
} 

出於某種原因的onClick不會被解僱,我意識到,wrapperChild是永遠等於findViewById(R.id.title1)。有任何想法嗎?

+0

你爲什麼這樣做?你爲什麼不使用'findViewById(...)'? – Squonk

+0

@MisterSquonk:我想動態地設置點擊處理程序,而不必爲儘可能多的視圖鍵入findViewById,這可能會很多。 – Phillip

+0

已回答:http://stackoverflow.com/questions/7394560/why-dont-inflated-views-respond-to-click-listeners – Phillip

回答

0

我能想到的兩種情況:

  • 首先,動態創建佈局。在這種情況下,當你創建TextView時,你可以設置你想要的OnClickListener,並且你已經準備好了。

  • 其次,你有一個特定的佈局,你可以有特定的ID並通過findViewById獲得對視圖的訪問,甚至可以通過android:onclick將特定的偵聽器直接設置到xml。

在任何情況下,代替本wrapperChild.setOnClickListener(this);的試試這個:wrapperChild.setOnClickListener(new OnClickListerer(){ // Code here});。希望這可以幫助!

+0

它對您有幫助嗎? –