Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Focus to EditText Android

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..

like image 880
Prexx Avatar asked Jul 22 '11 18:07

Prexx


People also ask

How do I change from focus to EditText?

This solution works (no need to add android:focusable="true"\android:focusableInTouchMode="true"): final EditText userEditText = (EditText)findViewById(R. id.

How do I remove focus in EditText?

Remove focus but remain focusable: editText. setFocusableInTouchMode(false); editText. setFocusable(false); editText.

How can I tell if EditText has focus Android?

You can use View. OnFocusChangeListener to detect if any view (edittext) gained or lost focus.

How do I change the focus to next text in Android?

setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (edtPasscode3. getText(). length() == 1) edtPasscode4. requestFocus(); return false; } });


1 Answers

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;
    }
});
like image 121
Marc Juschkeit Avatar answered Oct 17 '22 21:10

Marc Juschkeit