Single Click and Double Click in EditText using th

2019-09-05 14:06发布

问题:

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?

回答1:

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


回答2:

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


回答3:

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.