Mostrar el teclado para edittext cuando se inicia el fragmento
Cuando empiece mi fragmento, quiero que mi edittext esté enfocado / deje que el usuario empiece a escribir. Puedo conseguirlo en foco con requestFocus (), pero no puedo conseguir el teclado para aparecer.
He intentado ambos esto:
- La acción del método de entrada de EditText no funciona cuando se establece imeActionLabel
- Cómo limitar el texto en números sólo de 0-59 en Editar texto en Android?
- Eliminar todo Spannable con retroceso
- Después de pulsar una tecla, la vista desciende, sólo en S3
- EditText con SpannableStringBuilder y ImageSpan no funciona bien
edit = (EditText) view.findViewById(R.id.search); edit.requestFocus(); InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imgr.showSoftInput(edit, 0);
y
edit = (EditText) view.findViewById(R.id.search); InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imgr.showSoftInput(edit, 0); edit.requestFocus();
¿Cómo puedo obtener el teclado para aparecer en EditText?
- Teclado suave de Android cubre el campo edittext
- Detectar cambios en EditText (TextWatcher ineficaz)
- Xamarin C # EditText InputType Contraseña
- Validación de Android Email en EditText
- Implementación de Text Watcher para EditText
- Android Edittext- Expansión de vanos
- Android Lollipop EditText: Autocompletar no funciona para direcciones de correo electrónico
- Android: ¿Cómo rastrear si el teclado virtual está abierto?
¿Esto funciona?
imgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Puedes probar esto
@Override public void onResume() { super.onResume(); edit.post(new Runnable() { @Override public void run() { edit.requestFocus(); InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imgr.showSoftInput(edit, InputMethodManager.SHOW_IMPLICIT); } }); }
Como Nilzor dijo que esto funciona
imgr.showSoftInput(getView(), InputMethodManager.SHOW_IMPLICIT)
Y estoy de acuerdo en que es una mejor solución que toogleSoftInput
Dado que el uso de showSoftInput
no funciona para todos los casos y después de probar algunas de las soluciones mencionadas aquí, como:
if (binding.account.requestFocus()) { getActivity().getWindow() .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); }
Finalmente lo hice funcionar usando :
if (binding.account.requestFocus()) { ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput( InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY ); }
Ya que:
binding.account.requestFocus()
Sólo solicita el foco para el EditText
(no abre el teclado)
y
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput( InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY );
Es la única solución que parece estar funcionando correctamente para mostrar el teclado (y el más votado)
¡Buena suerte! Todos los derechos reservados
Otra forma de hacer que el teclado se abra cuando empieza el fragmento es llamar a requestFocus()
en onCreateView
y reaccionar en consecuencia abriendo el teclado si y sólo si el EditText
es EditText
.
if(this.editText.requestFocus()) { getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); }
@Override public void onHiddenChanged (boolean hidden) { super.onHiddenChanged(hidden); if(hidden) { hideKeyboard(yourView); } else { toggleKeyboard(yourView); } } public static void toggleKeyboard(View v) { InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); v.requestFocus(); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS); } public static void hideKeyboard(View v) { InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); }
Simplemente, usando la adición de 2 líneas funcionará como un encanto:
Si utiliza XML
android:focusable="true" android:focusableInTouchMode="true"
Else en Java:
view.setFocusableInTouchMode(true); view.requestFocus();
Este artículo me ayudó
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_edit_name, container); editText = (EditText) view.findViewById(R.id.txt_yourName); editText.requestFocus(); getDialog().getWindow().setSoftInputMode( LayoutParams.SOFT_INPUT_STATE_VISIBLE); return view; }
- ¿Cómo puedo mantener mi pantalla desbloqueada durante la depuración USB?
- Spanning columnas con TableLayout