suman
suman

Reputation: 75

OnTextChang for edittext in android?

I have 8 edit texts each allow only one character.when user enter something it automatically moves to next edit text. It's work fine by using onTextchanged.But i need to move backward also like when user enter back button it automatically moves to previous edit text and when enter something it move again next edit text. How i can do it.Please can any one help.

Any help would be highly appreciated.

editText_Pin1.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start,int before, int count) {
                if(editText_Pin1.getText().toString().length()==1) {    //size as per your requirement {
                    editText_Pin2.requestFocus();
                }

            }
            public void beforeTextChanged(CharSequence s, int start,int count, int after) {

            }
            @Override
            public void afterTextChanged(Editable arg0) {

            }
         });

Upvotes: 0

Views: 122

Answers (3)

britzl
britzl

Reputation: 10242

You have several options:

  1. Activity.onBackPressed()
  2. Activity.onKeyDown() or Activity.onKeyUp()
  3. View.setOnKeyListener() on each of your EditTexts
  4. View.onKeyPreIme()

I'd probably go with #1 or #3

Upvotes: 0

Android Killer
Android Killer

Reputation: 18489

you can do it by oveeriding the onBackPressed method

@Override
public void onBackPressed() {
    //check here which edittext has current focus,change the focus to previous edittext
    //you can use edittext array also.And also check if it is the first edittext then do nothing
    return;
}

Upvotes: 0

CRUSADER
CRUSADER

Reputation: 5472

Try something like this...

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if(editText_Pin2.getText().toString().length()==1){//Here you will check the last editField which has lenght == 1 
                        editText_Pin1.requestFocus();
                        return false;//This will make sure your activity doesn't gets finished
            }
       }
        return super.onKeyDown(keyCode, event);
    }

Hope this helps..

Upvotes: 1

Related Questions