GestureListener不与Android合作:自动连结= QUOT;电子邮件" [英] GestureListener not working with android:autoLink="email"

查看:140
本文介绍了GestureListener不与Android合作:自动连结= QUOT;电子邮件"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的布局xml文件。

  ...
< RelativeLayout的
 机器人:layout_width =FILL_PARENT
 机器人:layout_height =FILL_PARENT
 机器人:ID =@ + ID / RLT>    <的TextView
    机器人:ID =@ + ID / title_text
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_width =WRAP_CONTENT
    机器人:TEXTSIZE =22dp
    机器人:layout_centerHorizo​​ntal =真
    机器人:文字颜色=#add8e6
    /><的TextView
 机器人:ID =@ + ID /说明
 机器人:layout_width =FILL_PARENT
 机器人:layout_height =FILL_PARENT
 机器人:layout_below =@ + ID / tatle_text
 机器人:layout_marginTop =45dp
 机器人:文字颜色=#e3e4fa
 机器人:自动链接=电子邮件
 机器人:textColorLink =#fdd017
  />
     ...
     ...
     ...
     ...

结果
 这是我的 onFling():结果

  SimpleOnGestureListener simpleOnGestureListener
           =新SimpleOnGestureListener(){
          公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,
           浮动velocityY){
          // TODO自动生成方法存根             最终浮动xDistance = Math.abs(e1.getX() - e2.getX());
              最终浮动yDistance = Math.abs(e1.getY() - e2.getY());              如果(xDistance> gs.swipe_Max_Distance || yDistance> gs.swipe_Max_Distance)
              返回false;
               Log.v(Help_developers,丢宝贝!);
              velocityX = Math.abs(velocityX);
              velocityY = Math.abs(velocityY);
                    布尔结果= FALSE;              如果(velocityX> gs.swipe_Min_Velocity和放大器;&安培; xDistance> gs.swipe_Min_Distance){
               如果(e1.getX()> e2.getX())//从右到左
               {//滑动到Help_app
                   意图I =新意图(Help_developers.this,Help_app.class);
                   startActivity(ⅰ);
                完();
               }
               其他
               {
                 //滑动到Help_qanda
                   意图I =新意图(Help_developers.this,Help_pending.class);
                   startActivity(ⅰ);
               完();
               }               结果=真;
              }
               返回结果;
         }
    });
 最后GestureDetector gestureDetector
       =新GestureDetector(simpleOnGestureListener);
      rlt.setOnTouchListener(新View.OnTouchListener()
         {
                公共布尔onTouch(查看视图,MotionEvent事件){
                    Log.d(测试,点击!);
                    如果(gestureDetector.onTouchEvent(事件)){
                        Log.d(测试,手势检测);
                        返回true;
                    }                    返回false;
                }
            });

结果
RLT是RelativeLayout的 RLT 的ID。结果
现在在 onFling()属性的Andr​​oid不起作用急张:自动链接=电子邮件,在id为文本视图 说明。但是,当我删除该属性,一扔工作。我不知道为什么会这样。该属性如何影响一扔手势?
结果
修改结果
该onTouch()的 GestureListener 本身不叫。这些日志记录永远不会在logcat中。当属性android:自动链接=电子邮件是present。
搜索结果
布局完整的XML文件:

 <?XML版本=1.0编码=UTF-8&GT?;
    <滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
                 的xmlns:广告=htt​​p://schemas.android.com/apk/lib/com.google.ads
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>
     < RelativeLayout的
     机器人:layout_width =FILL_PARENT
     机器人:layout_height =FILL_PARENT
     机器人:ID =@ + ID / RLT>        <的TextView
        机器人:ID =@ + ID / title_text
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =WRAP_CONTENT
        机器人:TEXTSIZE =22dp
        机器人:layout_centerHorizo​​ntal =真
        机器人:文字颜色=#FFFFFF        />    <的TextView
     机器人:ID =@ + ID /说明
     机器人:layout_width =FILL_PARENT
     机器人:layout_height =WRAP_CONTENT
     机器人:layout_below =@ + ID / tatle_text
     机器人:layout_marginTop =45dp
     机器人:文字颜色=#e3e4fa
     机器人:自动链接=电子邮件
     机器人:textColorLink =#fdd017
     机器人:
      />
<查看
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:ID =@ + ID / rlt_touch/>    <按钮
            机器人:ID =@ + ID /家
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentRight =真
            机器人:layout_alignParentTop =真
            机器人:背景=@绘制/家
            机器人:文字=家
          />    < / RelativeLayout的>    < /滚动型>


解决方案

而不是关闭触摸在TextView的处理在我的的第一个答案,我们可以添加一个透明的视图(如RelativeLayout的的最顶层子)来捕捉触摸事件,使我们可以选择性地转发相关事件的TextView的同时还获得所有事件以检测手势:


  • 修改XML布局,添加触摸捕获查看:

      ...
    < RelativeLayout的
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:ID =@ + ID / RLT>    <的TextView
            机器人:ID =@ + ID / title_text
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_width =WRAP_CONTENT
            机器人:TEXTSIZE =22dp
            机器人:layout_centerHorizo​​ntal =真
            机器人:文字颜色=#add8e6
            机器人:背景=#FF0000/>    <的TextView
            机器人:ID =@ + ID /说明
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:layout_below =@ ID / title_text
            机器人:layout_marginTop =45dp
            机器人:文字颜色=#e3e4fa
            机器人:textColorLink =#fdd017
            机器人:自动链接=电子邮件/>    <! - 我们的触摸式摄像视 - >
        <查看
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:ID =@ + ID / rlt_touch/>< / RelativeLayout的>
    ...


  • 修改Java code,我们OnTouchListener设置为添加 rlt_touch 视图,而不是和添加事件转发code:

     最后查看RT = findViewById(R.id.rlt_touch);
    最终的TextView电视=(的TextView)findViewById(R.id.description);rt.setOnTouchListener(新OnTouchListener(){
        最后矩形R =新的矩形();
        最后一点P1 =新点();
        最后点P2 =新点();    @覆盖
        公共布尔onTouch(视图V,MotionEvent事件){
            Log.d(测试,点击!);        如果(gestureDetector.onTouchEvent(事件)){
                Log.d(测试,手势检测);
            }        //偏移触摸位置到TextView的,如果相关的转发
            rt.getGlobalVisibleRect(R,P2);
            tv.getGlobalVisibleRect(R,P1);
            event.offsetLocation(p2.x - p1.x,p2.y - p1.y);
            如果(r.contains((int)的event.getRawX(),(INT)event.getRawY())){
                tv.onTouchEvent(事件);
            }        返回true;
        }
    });


This is my Layout xml file.

     ...
<RelativeLayout 
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:id="@+id/rlt" >

    <TextView
    android:id="@+id/title_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" 
    android:textSize="22dp"
    android:layout_centerHorizontal="true"
    android:textColor ="#add8e6"
    />  

<TextView 
 android:id="@+id/description"   
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:layout_below="@+id/tatle_text"
 android:layout_marginTop="45dp"
 android:textColor = "#e3e4fa"
 android:autoLink="email"
 android:textColorLink="#fdd017"
  />
     ...
     ...
     ...
     ...


This is my onFling() :

   SimpleOnGestureListener simpleOnGestureListener
           = new SimpleOnGestureListener(){
          public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
           float velocityY) {
          // TODO Auto-generated method stub

             final float xDistance = Math.abs(e1.getX() - e2.getX());
              final float yDistance = Math.abs(e1.getY() - e2.getY());

              if(xDistance > gs.swipe_Max_Distance || yDistance > gs.swipe_Max_Distance)
              return false;
               Log.v("Help_developers", "Flinging baby!");
              velocityX = Math.abs(velocityX);
              velocityY = Math.abs(velocityY);
                    boolean result = false;

              if(velocityX > gs.swipe_Min_Velocity && xDistance > gs.swipe_Min_Distance){
               if(e1.getX() > e2.getX()) // right to left
               { //Slide to Help_app
                   Intent i=new Intent(Help_developers.this,Help_app.class);
                   startActivity(i);
                finish();
               }
               else
               {
                 //Slide to Help_qanda
                   Intent i=new Intent(Help_developers.this,Help_pending.class);
                   startActivity(i);
               finish();
               }

               result = true;
              }


               return result;
         }
    });
 final GestureDetector gestureDetector
       = new GestureDetector(simpleOnGestureListener);
      rlt.setOnTouchListener(new View.OnTouchListener()
         {
                public boolean onTouch(View view, MotionEvent event) {
                    Log.d("test", "clicked!");
                    if(gestureDetector.onTouchEvent(event))  {
                        Log.d("test", "gesture detected");
                        return true;
                    }

                    return false;
                }
            });


rlt is the id of RelativeLayout rlt.
Now the flinging in the onFling() does not work with the attribute android:autoLink="email" in the text view with id description. But when I remove that attribute, the fling works. I have no idea why this is happening. How can that attribute affect the fling gesture?
EDIT
The onTouch() of the GestureListener itself is not called. The logs are never recorded in the logcat. When the attribute android:autoLink="email" is present.

Complete Layout XML File :

  <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
                 xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >


     <RelativeLayout 
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:id="@+id/rlt" >

        <TextView
        android:id="@+id/title_text"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" 
        android:textSize="22dp"
        android:layout_centerHorizontal="true"
        android:textColor ="#ffffff"

        />  

    <TextView 
     android:id="@+id/description"   
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_below="@+id/tatle_text"
     android:layout_marginTop="45dp"
     android:textColor = "#e3e4fa"
     android:autoLink="email"
     android:textColorLink="#fdd017"
     android:
      />
<View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rlt_touch" />

    <Button
            android:id="@+id/Home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/home"
            android:text="Home"
          />

    </RelativeLayout>

    </ScrollView>

解决方案

Instead of turning off the touch handling in the TextView as in my first answer, we can add a transparent View (as the topmost child of the RelativeLayout) to capture the touch events so that we can selectively forward relevant events to the TextView while still getting all the events to detect the gestures:

  • Modify the XML layout, adding the touch-capturing View:

    ...
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rlt">
    
        <TextView
            android:id="@+id/title_text"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textSize="22dp"
            android:layout_centerHorizontal="true"
            android:textColor="#add8e6"
            android:background="#ff0000" />
    
        <TextView
            android:id="@+id/description"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/title_text"
            android:layout_marginTop="45dp"
            android:textColor="#e3e4fa"
            android:textColorLink="#fdd017"
            android:autoLink="email" />
    
        <!-- our touch-capturing view -->
        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/rlt_touch" />
    
    </RelativeLayout>
    ...
    

  • Modify the Java code, setting our OnTouchListener to the added rlt_touch View instead and adding the event forwarding code:

    final View rt = findViewById(R.id.rlt_touch);
    final TextView tv = (TextView) findViewById(R.id.description);
    
    rt.setOnTouchListener(new OnTouchListener() {
        final Rect r = new Rect();
        final Point p1 = new Point();
        final Point p2 = new Point();
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d("test", "clicked!");
    
            if (gestureDetector.onTouchEvent(event)) {
                Log.d("test", "gesture detected");
            }
    
            // offset touch location into the TextView and forward it if relevant
            rt.getGlobalVisibleRect(r, p2);
            tv.getGlobalVisibleRect(r, p1);
            event.offsetLocation(p2.x - p1.x, p2.y - p1.y);
            if (r.contains((int) event.getRawX(), (int) event.getRawY())) {
                tv.onTouchEvent(event);
            }
    
            return true;
        }
    });
    

这篇关于GestureListener不与Android合作:自动连结= QUOT;电子邮件&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆