2012-09-28 11 views
1

這是我的Layout xml文件。GestureListener不能使用android:autoLink =「email」

 ... 
<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" 
    /> 
    ... 
    ... 
    ... 
    ... 


這是我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是RelativeLayout的rlt的ID。
現在在onFling()中的投擲不能在屬性android:autoLink =「email」中使用,文本視圖中編號爲description。但是,當我刪除該屬性時,該投擲效果。我不知道爲什麼會發生這種情況。那個屬性如何影響這個投擲手勢?
EDIT
GestureListener本身的onTouch()不被調用。日誌從不記錄在logcat中。當屬性android:autoLink =「email」存在時。

佈局完整的XML文件:

<?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> 

回答

0

設置您的TextView android:autoLink="email"將使而開始使用觸摸事件,影響你的OnTouchListener是否會得到提供給它或不活動。

這裏有一些改動,你可以嘗試,讓您始終獲得事件:

  • 設置android:linksClickable屬性對你的TextView爲false:

    ... 
    android:autoLink="email" 
    android:linksClickable="false" 
    ... 
    
  • 修改您OnTouchListener總是返回true:

    rlt.setOnTouchListener(new OnTouchListener() { 
        @Override 
        public boolean onTouch(View v, MotionEvent event) { 
         Log.d("test", "clicked!"); 
         if (gestureDetector.onTouchEvent(event)) { 
          Log.d("test", "gesture detected"); 
         } 
         return true; 
        } 
    }); 
    
+0

嘗試了你的第二個方法。不起作用。仍然沒有檢測到觸摸。我不明白第一種方法。我剛剛粘貼了該代碼。它表示setMovemntMethod()未定義爲relativeLayout。 – Ashwin

+0

我的壞,不知何故,我的印象是'rlt'指的是TextView而不是RelativeLayout。讓我修改我的答案:) – Joe

+0

答覆修改後,我認爲您需要兩項更改才能生效。 – Joe

1

不像我的first answer那樣關閉TextView中的觸摸處理,我們可以添加一個透明視圖(作爲RelativeLayout的最頂層子)來捕獲觸摸事件,以便我們可以選擇性地將相關事件轉發給TextView還是讓所有的事件檢測手勢:

  • 修改XML佈局,添加觸摸捕獲查看:

    ... 
    <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> 
    ... 
    
  • 修改Java代碼,我們OnTouchListener設置爲添加rlt_touch的Vie w ^代替並添加事件轉發代碼:

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

對不起,仍然無法正常工作。爲什麼textview會消耗textview所有部分的觸摸事件。它僅僅消耗觸摸電子郵件部分的事件並將其餘的觸摸事件傳遞給GuestureListener就足夠了。 – Ashwin

+0

它在我的工作,不知道爲什麼它不適合你。也許你可以用完整的佈局XML更新你的答案? – Joe

+0

發佈完整的XML文件。 – Ashwin

相關問題