我在 Android 2.3.3 上处理 TextView
内的触摸事件时遇到问题。我已经使用 Activity 实现了 OnTouchListener
方法
main.java
public boolean onTouchEvent(MotionEvent event) {
if(event.getPointerCount()==1){
textView.setTextSize(mRatio+13);
mRatio++;
Log.d("TouchEvent", "one touch !");
}
if (event.getPointerCount() == 2) {
some code...
}
return true;
}
和我的布局(仅部分):
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1.0"
android:fadingEdge="none"
android:background="@color/white">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp">
<!-- TEXT EMAIL : -->
<TextView
android:id="@+id/mail_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="15sp"
android:autoLink="web"
android:clickable="true"
android:textColor="@color/mailtext"
android:scrollbars = "vertical"/>
</LinearLayout>
</LinearLayout>
当我“单击”文本框之外的任何位置(例如页眉或页脚)时,触摸事件触发,字体变大。我认为问题可能是因为滚动条。但是当我切断 android:scrollbars = "vertical"
栏就消失了。
这个textView通常包含大量文本。
如何在此 textView 内正确触发 onTouchEvents。
编辑: 当这个 textView 很小时,我的 touchEvent 会工作,直到我得到的文本很大,然后才需要滚动条。然后所有触摸事件都会被覆盖,您只能滚动 textView。没有调用 TouchEvent。
最佳答案
我用其他方式处理这个问题。 OnTouchEvent 不适用于我的 TextView,所以我重写了dispatchTouchEvent。执行“onTouch”操作要好得多。滚动条正在工作,我可以在整个屏幕上添加多点触控事件。它看起来像:
@Override
public boolean dispatchTouchEvent(MotionEvent motionEvent){
Log.v("NotificationActivity.dispatchTouchEvent()", "got touch");
if (motionEvent.getPointerCount() == 2) {
Log.v("touch", "multi touch !");
int action = motionEvent.getAction();
int pureaction = action & MotionEvent.ACTION_MASK;
// some actions ...
}
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:{
//Keep track of the starting down-event.
Log.v("akcjaa","down");
// some actions ...
break;
}
case MotionEvent.ACTION_UP:{
//Consume if necessary and perform the fling / swipe action
//if it has been determined to be a fling / swipe
Log.v("akcjaa","up");
break;
}
}
return super.dispatchTouchEvent(motionEvent);
}
关于java - 带滚动条的 TextView 可以处理 onTouchEvents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19181261/