如何在触摸事件上调用特殊区域?

时间:2011-05-28 18:07:34

标签: android layout button touch ontouchlistener

我的程序中有下一个UI:ui。 我想在按钮组上处理手指滑动事件,当然,不会失去点击按钮的能力。怎么做?

1 个答案:

答案 0 :(得分: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>