android – Duplicate focus when using requestFocus on an EditText

Question:

I created a screen with 2 EditText fields, one with numeric input and the other text. What I intend is to perform a validation on the first field when it loses focus, and if it's invalid, return the focus to this first field to be retyped.

The problem is when I use .requestfocus() , the two fields have the focus cursor, even using .clearfocus() in the second field, the cursor is only in the first one but if I type something it takes over the keyboard and the input of data in the second field.

For me it should be obvious when I request the focus of a field, this one should be selected assuming its settings and automatically deactivate the other one. Below is a very simple example. Thanks to anyone who can enlighten me.

package com.example.android.teste;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
EditText numero, texto;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    numero = findViewById(R.id.editText);
    texto = findViewById(R.id.editText2);
    numero.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (!b) {
            //Executo uma validação e conforme resultado inválido retorno no campo para redigitar.
            numero.requestFocus();
            }
        }
    });
    }
}

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="32dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="32dp"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Name"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

Answer:

Change it to something like this:

public class MainActivity extends AppCompatActivity {
    EditText numero, texto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        numero = findViewById(R.id.editText);
        texto = findViewById(R.id.editText2);

        texto.setOnFocusChangeListener((view, hasFocus) -> {
            if (!numeroPassouNaValidacao() && hasFocus) {
                view.clearFocus();
                esconderTeclado(view);
                numero.requestFocus();
                mostrarTeclado(numero);
            }
        });

    }

    private boolean numeroPassouNaValidacao() {
        return false;
    }


    private void esconderTeclado(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    private void mostrarTeclado(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, 0);
    }



}

What solved was a combination of passing the listener to the other EditText (which can be work if there are several fields, but it was the only way it solved), applying clearFocus() on the View that just received the focus, hiding the keyboard manually (otherwise it stays on the screen), do the requestFocus() in the number field and have the keyboard shown.

Scroll to Top
AllEscort