Autofocus in Edittext does not work

Asked

Viewed 89 times

1

I have a EditText that always needs to be in focus, and a Button whichever:

<EditText
        android:id="@+id/edtCod"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number" />
<Button
        android:id="@+id/btnLimpar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="limpar"
        android:text="Limpar" />

When I execute the method, it does not respect the requestfocus() and "jump" to the button.

Method:

public void confere(View view) {

     String codigo = cod.getText().toString();
     try {
         salvar(codigo);
         cod.setText("");
         cod.requestFocus();
     } catch (IOException e) {
         e.printStackTrace();
     }
}

Setando no onCreate:

this.limpar.setFocusable(false);

That way it works. But if I have several elements (Button, Layout, etc), I need to keep setFocusable(false), and sometimes even setando everything, does not work.

It would have been possible to use only the requestfocus() in the EditText, without having to set other things ?

1 answer

1

You can put in the edittext the field that will receive the next focus, thus:

android:nextFocusDown="@id/seuEdittext"

If your focus is set to the top, do so:

android:nextFocusUP="@id/seuEdittext"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.