LongClick event happens too quickly. How can I increase the clicktime required to trigger it?

后端 未结 8 1519
暖寄归人
暖寄归人 2020-12-08 21:26

In an application I\'m working on, I have the requirement that a user must click & hold a component for a period time before a certain action occurs.

I\'m curren

8条回答
  •  旧巷少年郎
    2020-12-08 22:05

    I found this method. It is so simple.

    private static final long HOLD_PRESS_TIME = 1200; //ms unit    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_control);
    
        //Declare your button and add listener
        ImageView iconImg = findViewById(R.id.more_icon);
        iconImg.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        Log.w("Button", "Button is pressed...");
                        //Start timer
                        timer.start();
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.w("Button", "Button is released...");
                        //Clear timer
                        timer.cancel();
                        break;
                }
                return false;
            }
        });
    
    
    }
    
    private CountDownTimer timer = new CountDownTimer(HOLD_PRESS_TIME, 200) {
        @Override
        public void onTick(long l) {
            Log.w("Button", "Count down..."+l); //It call onFinish() when l = 0
        }
    
        @Override
        public void onFinish() {
            //TODO your action here!
            Log.w("Button", "Count finish...");
    
        }
    };
    

提交回复
热议问题