簡體   English   中英

如何使用 OnTouchListener 打開新的 Activity

[英]How To Use OnTouchListener To Open New Activity

我嘗試了 OnClickListener 和 OnLongClickListener 是的,它起作用了,但是它們太快了,我想讓它們更長,我只是無法使用 OntouchListener 打開新活動而且我不知道幾乎嘗試了一切都沒有奏效

活動名稱:網站

按鈕 id:action_button(它是一個 floatingActionbutton)

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            FloatingActionButton actionButton = findViewById(R.id.action_button);
    
            defineView();
            handleIntent();
            defineActionBar();
            checkPermission();
                      
                       //i tried both here 
    
    
        public void openWebsite() {
            Intent intent = new Intent(this, Website.class);
            startActivity(intent);

不確定為什么要使用 OnTouchListener 而不是 OnClickListener,因為我認為它們在引用按鈕事件時沒有任何區別。

whit OnClickListener 您可以通過以下方式捕獲長按:

    button.setOnLongClickListener {
        //ACTION
        
        true
    }

編輯:(對於自定義持續時間,您應該使用 OnTouchListener,沒錯)

你可以這樣做:

  long time = 0;

  button.setOnTouchListener(new View.OnTouchListener() {
  @Override
  public boolean onTouch(View view, MotionEvent motionEvent) {
       if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
            time = System.currentTimeMillis();
       }
       else if(motionEvent.getAction() == MotionEvent.ACTION_UP){
            double duration = (System.currentTimeMillis() - time / 1000.0);
            if(duration > 5){
                action2();
                return true;
            }else if(duration > 3.2){
                action1();
                return true;
            }
        }
        return false;
        }
   });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM