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