Question

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

Was it helpful?

Solution

in each edittext in layout.XML just use

android:inputtype="text"

android:digits="abcdefghijklmnopqrstuvwxyz."

OTHER TIPS

Firstly since you want your user only to input characters not number then give android:inputtype="text" so that the edittext can only take text not numbers and then give android:maxLength="1" so that only one character can be inputted.

when on-click of an ediitext to remove/flush the contents in an edittext you have to implement the onCLick() method of your edittext and do : edittext.setText(""). You also should focus the edittext which you want the focus to be at by the .requestFocus()

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