I have the following xml layout:
<?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/开发者_C百科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>
And I would like to set a click listener for TextViews "title1", "title2", and "title3", by looping though and finding the corresponding View children:
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;
}
For some reason the onClick isn't being fired, and I realized that wrapperChild is never equal to findViewById( R.id.title1 ). Any ideas?
I can think of two cases:
First, you dynamic create the layout. In this case, when you create the TextView you can set the OnClickListener you want and you are ready.
Second, you have a specific layout where either you can have specific ids and get access to the Views by findViewById or even set specific listeners straight to the xml via android:onclick.
In any case, instead of this wrapperChild.setOnClickListener( this );
try this: wrapperChild.setOnClickListener(new OnClickListerer(){ // Code here});
. Hope this helps!
精彩评论