Single Click and Double Click in EditText using the click listener

我的梦境 提交于 2019-12-25 07:49:26

问题


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.



来源:https://stackoverflow.com/questions/41586072/single-click-and-double-click-in-edittext-using-the-click-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!