Search code examples
androidfocuseditbox

Android Edit box focus


NewBie Androider here.

I am writing a code in which user enters only one ALPHABET in one edit box then focus moves to other one. If it is not Alphabet then cursor will remain at edit box 1 If user delete the character then cursor remain at the same edit box.

Also if user click on editbox having some character in it then it should get removed when clicked.

Below is my attempt

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_play);
          final EditText et1 = (EditText)findViewById(R.id.box1);
          final  EditText et2 = (EditText)findViewById(R.id.box2);

        et1.addTextChangedListener(new TextWatcher()
        {
            public void afterTextChanged(Editable s){
            }
            public void beforeTextChanged(CharSequence s,
            int start, int count, int after){

            }
            public void onTextChanged(CharSequence s,
            int start, int before, int count)
            {
                if(isAlphabet(et1.getText().toString())){
                    et2.requestFocus();
                }else{
                    et1.setText("");
                    et1.requestFocus();
                }
            }
        });
    }

Also I have four edit box and I want to focus on the next empty box... any suggestion to implement this logic.

Thanks


Solution

  • in each edittext in layout.XML just use

    android:inputtype="text"

    android:digits="abcdefghijklmnopqrstuvwxyz."