如何让click事件传递给android中的容器?

我有一个FrameLayout表,其中每个帧包含 ImageView或TextView.无论帧中的内容如何,​​我都希望通过帧上的OnClickListener检测onClick事件.

我怎样才能做到这一点?

这是我的一些布局,我总共有5行(这里只显示1行).
如上所述,每行有5个FrameLayouts,每个包含一个TextView.我无法将OnClickListener放在TextView上,因为这可能会在运行时更改为ImageView.因此我想在FrameLayout上使用OnClickListener.

似乎FrameLayout的内容阻止它检测到click事件.

<TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/gateContainer"
        android:stretchColumns="*">

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center">

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/door1"
                android:clickable="true">

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="New Text"
                    android:id="@+id/textView"
                    android:layout_gravity="center" />

            </FrameLayout>

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/door2" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="New Text"
                    android:id="@+id/textView5"
                    android:layout_gravity="center" />
            </FrameLayout>

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/door3" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="New Text"
                    android:id="@+id/textView4"
                    android:layout_gravity="center"/>
            </FrameLayout>
     </TableLayout>

这是我如何设置OnClickListeners的示例:

View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v){
                // do something
            }
        };

        TableLayout tableLayout = (TableLayout) findViewById(R.id.gateContainer);
        // Go through all TableRows
        for (int i = 0; i < tableLayout.getChildCount(); i++){
            TableRow tableRow = (TableRow) tableLayout.getChildAt(i);
            // Set listener for each FrameView
            for (int j = 0; j < tableRow.getChildCount(); j++){
                FrameLayout frame = (FrameLayout) tableRow.getChildAt(j);

                frame.setOnClickListener(clickListener);
            }
        }

解决方法

只需在ImageView和TextView组件上实现OnTouchListener并将它们放在一边即可传递它们.
<your_view>.setOnTouchListener(new OnTouchListener(){

    @Override
    public boolean onTouch(View v,MotionEvent event) {
        // TODO Auto-generated method stub
        return true;
    }

});

这将确保您的容器处理.

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...