Single Click and Double Click in EditText using th

2019-09-05 14:05发布

My requirement is, on single click on Edit Text, user can enter data, on double click go to an activity where all data will be present.

I used the logic for press again to exit, I am unable to achieve it.

        ETBarCode.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            doublePress=doubleTap();
            if(doublePress) {
                ETBarCode.requestFocus();
                InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(ETBarCode.getWindowToken(), 0);
            }
            else
            {
                Toast.makeText(MoveActivity.this, "Enter Data", Toast.LENGTH_SHORT).show();
                ETBarCode.requestFocus();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(ETBarCode, 0);

            }
        }
    });
}

private boolean doubleTap()
{
    if (doubleBackToExitPressedOnce) {
        Toast.makeText(this, "Scanning", Toast.LENGTH_SHORT).show();
        return doubleBackToExitPressedOnce;
    }
    this.doubleBackToExitPressedOnce = true;
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doubleBackToExitPressedOnce = false;
        }
    }, 2000);
    return doubleBackToExitPressedOnce;
}

Is there any way to sort it out?

3条回答
我只想做你的唯一
2楼-- · 2019-09-05 14:23

You can manage with counter variable:

int count = 0;

ETBarCode.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            count++;
            doublePress=doubleTap();
            if(count==1) {
              ShowData();
              count=0;

            }
            else if(count==2){
               StartNewActivity();
              count=0;
            }

        }
    });

}

Hope this will help you.

查看更多
戒情不戒烟
3楼-- · 2019-09-05 14:31

This should solve your problem. I tried changing as little as possible from your code, however some things are not clear (where exactly are you redirecting to the new activity?). Also, I would not recommend this double tap to proceed thing in terms of user experience, it is very unconventional. I would rather have it as clickable TextView (with design clearly suggesting the clickability) for the "view" action, and an image button (the good old android pencil) next to it for the "edit" action.

ETBarCode.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        if(isDoubleClick()) {
            ETBarCode.requestFocus();
            InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(ETBarCode.getWindowToken(), 0);
        }
        else {
            Toast.makeText(MoveActivity.this, "Enter Data", Toast.LENGTH_SHORT).show();
            ETBarCode.requestFocus();
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(ETBarCode, 0);
        }
    }
});

private boolean isDoubleClick() {
    if (!userHasClickedOnce) {
        Toast.makeText(this, "Scanning", Toast.LENGTH_SHORT).show();
        return false;
    }

    new Handler().postDelayed(new Runnable() {
    @Override
        public void run() {
            userHasClickedOnce = false;
        }
    }, 2000);

    return true;
}
查看更多
聊天终结者
4楼-- · 2019-09-05 14:34

Use GestureDetector to detect:

final GestureDetector gestureDetector = new GestureDetector(your_context,new GestureDetector.SimpleOnGestureListener() {
    public boolean onDoubleTap(MotionEvent e) {
        // start activity
        return true;
    }
});

EditText et = (EditText) findViewById(R.id.your_id);
et.setOnTouchListener(new View.OnClickListener() {
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
});
查看更多
登录 后发表回答