0

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

user1544460
  • 173
  • 7
  • 18
  • And, what is the problem with the code you have shown? – Cat Aug 18 '12 at 05:03
  • Problem is when I enter some Number its stop working. though my android:inputType="text|textCapCharacters|textNoSuggestions" still I can enter any number also if user click on editbox having some character in it then it should get removed when clicked and focus will be on same edit box. – user1544460 Aug 18 '12 at 06:55
  • Can you show the `isAlphabet` method? – Cat Aug 18 '12 at 06:57
  • public static boolean isAlphabet(String str) { return str.matches("[a-zA-Z]"); } THough this problem get solved by @hossam solution android:digits="abcdefghijklmnopqrstuvwxyz." with this I can only enter alphabets – user1544460 Aug 18 '12 at 08:01

2 Answers2

1

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()

Android2390
  • 1,150
  • 12
  • 21
  • android:inputType="text|textCapCharacters|textNoSuggestions" still I can enter any digits – user1544460 Aug 18 '12 at 06:27
  • also et1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et1.setText(""); et1.requestFocus(); } }); not working... crashing the app – user1544460 Aug 18 '12 at 06:47
  • Now OnClickListener is working... but I have to click two time to reset the value. here is the code et1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et1.setText(""); } } ); – user1544460 Aug 18 '12 at 08:53
1

in each edittext in layout.XML just use

android:inputtype="text"

android:digits="abcdefghijklmnopqrstuvwxyz."
Hossam Alaa
  • 663
  • 5
  • 9