2011-05-28 71 views
1

我的程序中有下一個UI:ui。 我想在一組按鈕上處理手指輕掃事件,當然,不會失去單擊按鈕的能力。怎麼做?如何爲觸摸事件調用特殊區域?

+0

你是說,你要處理它,如果在幾個按鈕或只是一個發生刷卡?當然,還有幾個當然是 – Maximus 2011-05-28 19:10:13

+0

。例如顯示寬度的60%。 – Divers 2011-05-28 19:32:57

回答

1

你說的是屏幕底部的按鈕?

下面是3個按鈕水平對齊的示例,可以輕掃並點擊。處理「onSingleTapUp」中的點擊和「onFling()」中的滑動。

public class SwipeExample extends Activity implements OnTouchListener { 

    private static final String TAG = SwipeExample.class.getName(); 

    private int mOnTouchId; 

    private GestureDetector mGestureDetector; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main);  

     mGestureDetector = new GestureDetector(new MyGestureDetector()); 

     Button one = (Button) findViewById(R.id.button_one);   
     one.setOnTouchListener(this); 
     Button two = (Button) findViewById(R.id.button_two); 
     two.setOnTouchListener(this); 
     Button three = (Button) findViewById(R.id.button_three); 
     three.setOnTouchListener(this);  
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     mOnTouchId = v.getId(); 
     if (mGestureDetector.onTouchEvent(event)) {     
      Log.d(TAG, "onTouchEvent"); 
     } 
     return false; 
    } 

    private class MyGestureDetector extends SimpleOnGestureListener { 
     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
       float velocityY) {   
      Log.d(TAG, "onFling() - MyGestureDetector"); 
      return true; 
     } 
     @Override 
     public boolean onDown(MotionEvent e) { 
      Log.d(TAG, "onDown() - MyGestureDetector"); 
      return true; 
     } 

     @Override 
     public boolean onSingleTapUp(MotionEvent e) { 
      Log.d(TAG, "onSingleTapUp() - MyGestureDetector"); 
      if (mOnTouchId == R.id.button_one) { 
       Log.d(TAG, "Button one"); 
      } else if (mOnTouchId == R.id.button_two) { 
       Log.d(TAG, "Button two"); 
      } else if (mOnTouchId == R.id.button_three) { 
       Log.d(TAG, "Button three"); 
      } 
      return true; 
     }  
    } 
} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="horizontal" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:id="@+id/LinearLayout" 
     android:background="#348282"> 
     <Button android:id="@+id/button_one" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:text="Button one" /> 
     <Button android:id="@+id/button_two" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:text="Button two" /> 
     <Button android:id="@+id/button_three" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:text="Button three" /> 
    </LinearLayout> 
+0

似乎觸摸事件將調用三次,如果用戶將手指輕掃所有按鈕。真正? – Divers 2011-05-29 16:43:37

+0

錯誤。當您首先在所有按鈕上滑動onDown,然後onFling將被調用。 – rochdev 2011-05-29 17:45:41