setOnClickListener()遇到问题

时间:2011-09-09 20:14:01

标签: android layout view listener

我有以下xml布局:

<?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”,“title2”和“title3”设置一个点击监听器,通过循环查找相应的View子项:

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)。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我可以想到两个案例:

  • 首先,动态创建布局。在这种情况下,当您创建TextView时,您可以设置所需的OnClickListener并准备就绪。

  • 其次,您有一个特定的布局,您可以通过findViewById获取特定ID并访问Views,甚至可以通过android:onclick直接将特定侦听器设置为xml。

在任何情况下,请代替此wrapperChild.setOnClickListener( this );尝试:wrapperChild.setOnClickListener(new OnClickListerer(){ // Code here});。希望这有帮助!