簡體   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