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

frm*_*rmi 6 layout android onclicklistener android-framelayout

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

我怎样才能做到这一点?

这是我的一些布局,我总共有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>
Run Code Online (Sandbox Code Playgroud)

这是我如何设置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);
            }
        }
Run Code Online (Sandbox Code Playgroud)

Tec*_*ist 7

只需在ImageView和TextView组件上实现OnTouchListener并将它们放在一边即可传递它们.

<your_view>.setOnTouchListener(new OnTouchListener(){

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

}); 
Run Code Online (Sandbox Code Playgroud)

这将确保您的容器处理.


gar*_*arh 7

您还可以在后台设置android:clickable ="false"到TextView/ImageView布局和android:clickable ="true",这样背景视图总能捕获点击次数.

此外,以下答案可能对考虑此问题的人有所帮助:

SO:Android:如何将click事件传播到LinearLayout子项并更改其drawable