Question

I have 4 edit texts; oct1, oct2, oct3, oct4. These edit texts are the type numberDecimal. I have initial focus on oct1. I need it so when the user presses . (numpad dot) it will not enter the . the oct, clear focus and focus on the next edit text.

This should be easy, but is surprisingly frustrating for me. This is what I have tried.

Overriding onKeyDown. This won't even produce the logs.

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
       if(keyCode == KeyEvent.KEYCODE_NUMPAD_DOT){
          oct3.clearFocus();
          oct4.requestFocus();
          return true;
       }
       Log.d("KeyCode", String.valueOf(keyCode));
       Log.d("event", String.valueOf(event));
       return false;
}

Then this. I have many different variants of this. I have been able to switch focus with this, but not delete the ..

    oct3.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //do nothing
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(count > 0){
                if(s.charAt(s.length()-1) == ".".charAt(0)){

                    Log.d("Sequence", "Noticed a \".\"");
                    isPeriod = true;
                } else {
                    Log.d("Sequence", "Noticed a \""+s+"\"");
                }
            }

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(isPeriod){
                s.delete(s.length() -1, s.length() -1);
                oct3.clearFocus();
                isPeriod = false;
                oct4.requestFocus();
            }
        }
    });
Was it helpful?

Solution

Try setting it in the onTextChanged:

  listener = (new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(count > 0){
                if(s.charAt(s.length()-1) == '.'){
                    Log.d("Sequence", "Noticed a \".\"");
                    CharSequence text = s.subSequence(0, s.length()-1);
                    Log.d("VIEW CLICKED:", String.valueOf(getCurrentFocus()));
                    setTextAndAdvance(getCurrentFocus(), text);
                } else {
                    Log.d("Sequence", "Noticed a \""+s+"\"");
                }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top