20

I have 2 EditText's on my Activity and set the maxLength to 5.

Now I want to set the focus to editText2, if the length of 5 is reached in editView1...

I tried it with:

editView1.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if(editView1.getText().length() == 5)
            editView2.requestFocus();
        return false;
    }
});

But it won't work..

Sam
  • 86,580
  • 20
  • 181
  • 179
Prexx
  • 2,959
  • 5
  • 31
  • 48

5 Answers5

43

This works

final EditText editText1 = (EditText) findViewById(R.id.editText1);
final EditText editText2 = (EditText) findViewById(R.id.editText2);

editText1.setOnKeyListener(new OnKeyListener() {

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if(editText1.getText().length() == 5)
            editText2.requestFocus();
        return false;
    }
});
Marc Juschkeit
  • 576
  • 5
  • 6
  • So i have to change my EditView's to EditText's? i will try it, thanks. – Prexx Jul 23 '11 at 12:54
  • 2
    Won't work... i recognized, that the event ist only fired, if i use the "backspace" key... – Prexx Jul 23 '11 at 13:09
  • 1
    Thats wired. I tryed that snip of code in a sample project and at did exactly what you want. After the 5th key it changed the focus to the other EditText. Maybe you had to override one of the other methodes from the OnKeyListener. Did you try this? – Marc Juschkeit Jul 25 '11 at 06:31
  • Text with the method addTextChangedListener(new android.text.TextWatcher(){ }); that object have three methods onTextChanged, beforeTextChanged, afterTextChanged, I think you need use afterTextChanged and put here the code if(editView1.getText().length() == 5) editView2.requestFocus(); – Maria Mercedes Wyss Alvarez Jun 17 '13 at 21:07
  • 1
    @Prexx I have your problem too. onKey event only fired when i use backspace key. How did you solved your problem? – Dr.jacky Jul 11 '14 at 08:14
2

Marc Juschkeits post almost was perfect for me.

But in my case i had to test if the keyevent is an action up because i have a standard text in the edittext that has the same length.

final EditText editText1 = (EditText) findViewById(R.id.editText1);
final EditText editText2 = (EditText) findViewById(R.id.editText2);

editText1.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {
            if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
                if (editText1.getText().length() == 5) {
                    editText2.requestFocus();
                }
            }
            return false;
        }
    });
klange
  • 3,465
  • 3
  • 16
  • 16
1

Request focus not working on main thread i think. But i'm not sure. This is worked for me,

_edittext.post(new Runnable() {
      public void run() 
        {
           _edittext.requestFocus();
        }

});
1

EdName.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            System.out.println("Yes Bud");

            if(EdName.length()== 5)
            {
                    EdBin.requestFocus();

            }
                return false;


        }
    });
Alexander Zaldostanov
  • 2,907
  • 4
  • 30
  • 39
-1

In the androidmanifest.xml file add the following code in activity tag:

android:windowSoftInputMode="stateHidden"
brightintro
  • 1,016
  • 1
  • 11
  • 16