开发者

How to make special zone for invoke on touch event?

I have next UI in my program: ui. I want to handle finger swipe event on set of the buttons, of course, without losing the ability to click on th开发者_开发技巧e button. How to do that?


You are talking about the buttons at the bottom of the screen?

Here is an example of 3 buttons aligned horizontally where it's possible to swipe and click. Handle the clicks in "onSingleTapUp" and the swipes in "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;
        }       
    }   
}

The 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜