繁体   English   中英

如何在Android中通过单击一个按钮来实现三个不同的事件?

[英]How to implement three different events with single button click in android?

我想为一个按钮处理三个不同的事件,例如当按钮被触摸,按下和释放时。 这怎么可能? 请帮助我。

到目前为止,这是我为处理两个事件而开发的代码,但我需要三个事件。

button.setOnTouchListener(new OnTouchListener() 
{
    public boolean onTouch(View v, MotionEvent event) 
    {

        // Check if the button is TOUCH
        if (event.getAction() == MotionEvent.ACTION_DOWN)  
        {

        }


        // Check if the button is RELEASED
        else if (event.getAction() == MotionEvent.ACTION_UP) 
        {   

        }


            return true;
        }
});

到目前为止,您开发的内容似乎还不错,应该可以很好地处理触摸和释放事件。 长按稍微复杂一点,但是一点也不花哨。 您最好使用GestureDetector 这个答案给出了一个例子。 或者,您可以查阅官方文档

更新:您可能会发现在手势官方教程非常有用。 我相信特别是public void onLongPress(MotionEvent event)方法将满足您的需求!

您将尝试使用此方法,希望它能在您的情况下正常工作...您只使用此Activity并使用此方法实现最佳效果...您仅使用下面的侦听器类实现长按。 ..

public class MainActivity extends Activity {

private Button button;  
int check=0;
private GestureDetector  detector;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button = (Button)findViewById(R.id.button1);
    detector = new GestureDetector(new GestureListener());
    check=0;
    button.setOnTouchListener(new OnTouchListener() {           
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            detector.onTouchEvent(event);
            int action = event.getActionMasked();               
            switch (action) {
            case MotionEvent.ACTION_MOVE:
                System.out.println("Move");
                break;
            case MotionEvent.ACTION_DOWN:
                System.out.println("Down");
                break;

            case MotionEvent.ACTION_UP:
                System.out.println("Released");
                break;              
            default:
                break;
            }
            return false;
        }
    });
}
class GestureListener implements OnGestureListener
{

    @Override
    public boolean onDown(MotionEvent e) {
        System.out.println("Down List");
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        System.out.println("Fly List");
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        System.out.println("Long press");

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
            float distanceX, float distanceY) {

        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

        System.out.println("Press List");

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        System.out.println("Single Tap List");
        return false;
    }

}
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM