Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何让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;
    }

});

这将确保您的容器处理.

大佬总结

以上是大佬教程为你收集整理的如何让click事件传递给android中的容器?全部内容,希望文章能够帮你解决如何让click事件传递给android中的容器?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。