问题描述
- OnTouchListener 不能使用 relative layout
-
我在一个RelativeLayout中动态的添加了很多TextViews。我现在面临的问题是:不论什么时候当我单独申请一个 onTouch 监听器到TextViews,当我添加touch事件到relative layout时它能检测到touch 事件,但是没有回应。
检测touch事件代码:
TextView tv = new TextView(this); tv.setText(values[i]); Drawable d = getResources().getDrawable(R.drawable.level_small_corners); tv.setClickable(true); tv.setId(i+1); tv.setTextSize(18); tv.setOnTouchListener(cellTouch);
但是我在 myRelativeLayout 添加所有的 TextViews
myRelativeLayout.setOnTouchListener(cellTouch);
现在 onTouchListener 也不能调用,为什么?
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/black" > . . . <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/levelFirstLayout" android:layout_marginBottom="70dp" > <RelativeLayout android:id="@+id/wordsRelativeLayout" android:layout_width="fill_parent" android:clickable="true" android:layout_height="wrap_content" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" > </RelativeLayout> </ScrollView> . . . </RelativeLayout>
解决方案
问题出在 tv.setClickable(true);
,删除这条语句。
解决方案二:
在 myRelativeLayout.xml 添加:
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
解决方案三:
加这个tv.setLongClickable(true);就可以了
时间: 2024-08-30 23:44:34