Sé que hay bastantes preguntas aquí con respecto a esta pregunta. Pero ninguno de ellos tiene la respuesta que estoy buscando.Borrar el foco EditText cuando se presiona el botón Atrás cuando el teclado está mostrando
Tengo 7 ET dentro de ScrollView. Cuando comienzo a la aplicación sin ET tiene el foco, ya he añadido las dos líneas siguientes al diseño general:
android:focusable="true"
android:focusableInTouchMode="true"
Cuando hago clic en un ET se muestra la softkeyboard, lo que yo quiero, entonces establecer el valor (digamos 20) Presiono un '2' seguido de un '0' y luego presiono el botón Atrás. En este punto, el teclado desaparece, pero el foco permanece. Me gustaría borrar el enfoque también cuando presiono el botón Atrás para ocultar el teclado.
Porque cuando se borra el enfoque, el diseño del ET se establece como yo quiero.
Todos ellos tienen acerca de la parte de código, que es:
// Right Cable
RightCable = (EditText) findViewById (R.id.RightCable);
RightCable.setRawInputType(Configuration.KEYBOARD_12KEY);
RightCable.setOnFocusChangeListener(FocusChanged);
RightCable.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(RightCable.isFocused()){
LengthRightCable = Double.parseDouble(RightCable.getText().toString());
Calculate();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.toString().matches("")) {
RightCable.setText("0.00");
Selection.setSelection(RightCable.getText(), 0, 4);
}
}
});
utilizo un oyente enfoque para cambiar la entrada del ET a un número como 5,00 en lugar de 0.
OnFocusChangeListener FocusChanged = new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
EditText et = (EditText) v;
et.setSelection(0, et.getText().length());
if(!hasFocus){
String userInput = et.getText().toString();
int dotPos = -1;
for (int i = 0; i < userInput.length(); i++) {
char c = userInput.charAt(i);
if (c == '.') {
dotPos = i;
}
}
if (dotPos == -1){
et.setText(userInput + ".00");
} else if(userInput.length() < 5) {
if (userInput.length() - dotPos == 1) {
et.setText(userInput + "00");
} else if (userInput.length() - dotPos == 2) {
et.setText(userInput + "0");
}
}
}
}
};
que han encontrado una solución para esto? – PrisonMike
Recomiendo encarecidamente la respuesta de Kacy en el enlace adjunto: http://stackoverflow.com/questions/13975058/make-edittext-lose-focus-on-back-press –