onSingleTapUp - 仅用于点击按钮

时间:2014-12-12 17:37:07

标签: android onclick gesture ontouchevent

我的Buttons中有两个Linear Layout。我成功覆盖了我onTouch的{​​{1}},这样当我扔掉它们时会发生一些事情。我还希望能够点击Buttons,因此我会覆盖Buttons

问题在于onSingleTapUp不仅适用于我的onSingleTapUp,也适用于我屏幕上的其他任何地方。那么有没有办法让Buttons仅引用onSingleTapUp而不是点击屏幕上的任何位置?

尝试:我注意到Buttons有一个布尔值MotionEvent,所以我尝试了以下操作,但它崩溃了我的应用程序:

isButtonPressed(int button)

这是我的完整代码:

类MainActivity:

@Override
public boolean onSingleTapUp(MotionEvent ev) {
    if (ev.isButtonPressed(1))
        Log.d("button", "override - click event performed");
    return true;
}

类GestureListener:

public class MainActivity extends Activity {

    protected Button bttn1, bttn2;
    private GestureDetector mGestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mGestureDetector = new GestureDetector(this, new GestureListener());

        bttn1 = (Button) findViewById(R.id.bttn1);
        bttn2 = (Button) findViewById(R.id.bttn2);

        bttn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("button1", "click event performed");
            }
        });

        bttn1.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                   if(mGestureDetector.onTouchEvent(event)) {

                        return true;
                    }
                    return false;

            }
        });

        bttn2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("button2", "click event performed");
            }
        });

        bttn2.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                   if(mGestureDetector.onTouchEvent(event)) {

                        return true;
                    }
                    return false;

            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {

        boolean eventConsumed=mGestureDetector.onTouchEvent(event);
            if (eventConsumed)
            {
                return true;
            }
            else
                return false;
    }

}

1 个答案:

答案 0 :(得分:0)

也许这会对你有所帮助:

public class MyTouchListener implements OnTouchListener{

protected static final String TAG = "MyTouchListener";
protected static final boolean D = true;

public interface MyListener{
    public void onTouch();
    public void onClick();
}

private MyListener mListener;
private GestureDetector mGestureDetector;


private class MyClickListener extends SimpleOnGestureListener{

    @Override
    public boolean onDown(MotionEvent e) {
        if (D){
            Log.d(TAG, "onDown");
        }
        return true;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent ev) {
        if (D){
            Log.d(TAG, "onSingleTapUp");
        }
        mListener.onClick();
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (D){
            Log.d(TAG, "onFling");
        }
        return true;
    }
}

public MyTouchListener(Context context, MyListener listener){
    mGestureDetector = new GestureDetector(context, new MyClickListener());
    mListener = listener;
}

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

}

您可以像这样使用它:

    Button b = (Button) findViewById(R.id.button);

    b.setOnTouchListener(new MyTouchListener(this, new MyListener() {

        @Override
        public void onTouch() {
            if (D){
                Log.d(TAG, "onTouch");
            }
        }

        @Override
        public void onClick() {
            if (D){
                Log.d(TAG, "onClick");
            }
        }
    }));