带有可以处理onTouchEvents的滚动条的TextView

时间:2013-10-04 12:33:35

标签: java android scrollbar ontouchevent

我在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>

当我点击'textBox外的任何地方(例如在页眉或页脚上)时,触摸事件触发器,字体变大。我假设问题可能是因为滚动条。但当我切断android:scrollbars = "vertical"栏时,就消失了。

此textView通常包含大量文本。

如何正确触发此textView中的onTouchEvents。

编辑: 当这个textView很小时,我的touchEvent工作,直到我得到文本这么大,比需要scrollBar。然后所有触摸事件都被覆盖,您只能滚动textView。没有调用TouchEvent。

2 个答案:

答案 0 :(得分:1)

我以其他方式处理问题。 OnTouchEvent无法使用我的TextView,因此我覆盖了dispatchTouchEvent。对“onTouch”执行动作要好得多.Scrollbar正在工作,我可以在整个屏幕上添加我的多点触控事件。它看起来像:

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

答案 1 :(得分:0)

我假设您的TextView位于某种ScrollView或Scrollable容器中,对吧?

然后你将面临这个问题。 Android不喜欢他的苹果和梨混合,你不应该试图接收视图顶部的父布局所消耗的“onTouch”事件。

我想你可以:

a)切换到TextView上的onClickListener

b)覆盖父视图布局触摸处理程序,将触摸向下渗透到其子项。这实际上不是100%推荐的,可能会导致不必要的行为。

很抱歉,如果我无法提供更多帮助。