Duplicate focus when using requestFocus in an EditText

3

I created a screen with 2 EditText fields, one with numeric entry and another text. What I want is to perform a validation in the first field when it loses focus, and if it is invalid, return the focus to that first field to be retyped.

The problem is when I use .requestfocus() , the two fields are with 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 the keyboard and the data entry in the second field.

For me it should be obvious when requesting the focus of a field, this be selected by assuming its settings and automatically deactivate the other. I put a simple example below. Thank you to anyone who can clarify.

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" />

    
asked by anonymous 23.12.2017 / 16:56

1 answer

1

Switch 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 might work if there were multiple fields, but it was the only way it solved), apply clearFocus() to the View that just got focus, hide the keyboard manually (otherwise it stays on the screen), make requestFocus() in the number field and display the keyboard.

    
23.12.2017 / 20:47