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

时间:2013-11-28 06:50:29

标签: android layout onclicklistener android-framelayout

我有一张FrameLayout表,其中每个框架包含ImageViewTextView。无论框架中的内容如何,​​我都希望框架上的onClick设置检测到OnClickListener事件。

我怎样才能做到这一点?

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

似乎FrameLayout的内容阻止它检测到点击事件。

    <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);
            }
        }

3 个答案:

答案 0 :(得分:7)

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

<your_view>.setOnTouchListener(new OnTouchListener(){

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

}); 

这将确保您的容器能够处理。

答案 1 :(得分:7)

你也可以设置android:clickable =&#34; false&#34;到TextView / ImageView布局和android:clickable =&#34; true&#34;在背景上,这样背景视图总能捕捉到点击次数。

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

SO: Android: How to propagate click event to LinearLayout childs and change their drawable

答案 2 :(得分:0)

您可以在TextViewImageView上定义点击事件,如下所示:

   <yourView>.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // your logic here.

        }
    });