简体   繁体   中英

How To Use OnTouchListener To Open New Activity

I Tried OnClickListener an OnLongClickListener And yea it worked but those are too quick and i want to make them even more longer and i am just unable to use OntouchListener To Open New Activity And i have no clue almost tried everything nothing worked

Activity Name: Website

Button id: action_button (its a 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);

Not sure why you want to use OnTouchListener instead of OnClickListener since I don't think there is any difference between them referring to button events.

whit OnClickListener you could capture long press with:

    button.setOnLongClickListener {
        //ACTION
        
        true
    }

EDIT: (for custom duration, you should use OnTouchListener, thats right)

you can do something like this:

  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;
        }
   });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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