¿Cómo ocultar el teclado virtual al cambiar las pestañas?
EDIT: Parece que no estoy claro. Lo que necesito es una forma de ocultar el teclado suave siempre que reemplace el fragmento en el que estoy. ¿Cómo hago para hacer esto?
Déjeme guardar esto simple. Tengo un cuadro de EditText en Fragmento de la lengüeta 1.2 que se abre obviamente el teclado suave cuando está presionado. ¿Cómo ocultar esto cuando se cambia la pestaña? He intentado lo siguiente en mi onTabSelected () que no parece hacer nada
- No se puede establecer una vista de lista personalizada en ListFragment
- Error: El método no estático 'findViewById (int)' no se puede referenciar desde un contexto estático
- FrameLayout no se puede convertir en android.widget.AbsListView
- La vista de pie de página para ListView no se muestra en 1 dispositivo, pero está en otro?
- Separador (divisor) después del último elemento de ListView
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Lo he intentado todo ahora. Ninguna de las soluciones sugeridas que he localizado hasta ahora me está ayudando de ninguna manera.
- Implementar la búsqueda en ListView dentro del fragmento
- Android Espresso ListView haga clic en el elemento
- ¿Qué hace el segundo parámetro en AddHeaderView en la clase ListView?
- Cómo utilizar ListFragment en Android Swipe Tabs?
- Establecer onClickListener a LinearLayout llenado por adaptador personalizado
- En Android SQLite, trabajar directamente con Cursor es más eficiente en la memoria que crear objetos de modelo?
- Android adapter.getItem (position) .getItemId () no funciona (El método getId () no está definido para el tipo Object)
- ¿Por qué ListView y RecyclerView son focusableInTouchMode?
De forma programática, puede utilizar, capturando la vista de la actividad activa en la pantalla del dispositivo.
public final void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) { View focus = getCurrentFocus(); if (focus != null) { hiddenKeyboard(focus); } } public final void onTabselected(Tab tab, FragmentTransaction fragmentTransaction) { View focus = getCurrentFocus(); if (focus != null) { hiddenKeyboard(focus); } } public final void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) { View focus = getCurrentFocus(); if (focus != null) { hiddenKeyboard(focus); } } private void hiddenKeyboard(View v) { InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); keyboard.hideSoftInputFromWindow(v.getWindowToken(), 0); }
Tuve el mismo problema y colocó el código siguiente en mi fragmento de la pestaña justo antes de utilizar el método FragmentTransaction.replace()
para cambiar las pestañas. Los métodos onCreate
y onCreateView
en cada fragmento no se activan después de la selección inicial de la ficha, por lo que ocultar el teclado se puede hacer antes de llegar a la clase del fragmento específico. Usar mTabHost.getApplicationWindowToken()
lugar de editText.getWindowToken()
fue una ayuda importante. Gracias a quien lo encontró. Perdón por haber perdido el enlace.
InputMethodManager im = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); im.hideSoftInputFromWindow(mTabHost.getApplicationWindowToken(), 0); ....... fm = getFragmentManager(); .... fm.beginTransaction() .replace(placeholder, new someFragment(), tabId) .commit();
Así es como habilita el teclado virtual
inputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
Y así se cierra cuando cambia de pestaña.
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
Establecer OnPageChangeListener a viewpager en mainActivity Donde agrega fragmentos a viewPager
viewPager.setOnPageChangeListener(myOnPageChangeListener); ViewPager.OnPageChangeListener myOnPageChangeListener = new ViewPager.OnPageChangeListener() { @Override public void onPageScrollStateChanged(int state) { //Called when the scroll state changes. } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { //This method will be invoked when the current page is scrolled, //either as part of a programmatically initiated smooth scroll //or a user initiated touch scroll. hideKeyboard(); } @Override public void onPageSelected(int position) { //This method will be invoked when a new page becomes selected. //hide keyboard when any fragment of this class has been detached hideKeyboard(); } }; public void hideKeyboard() { InputMethodManager inputManager = (InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); // check if no view has focus: View v = getCurrentFocus(); if (v == null) return; inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0); }
En su archivo XML simplemente quite android:focusable="true"
desde cualquier lugar.
Una cosa más es si está utilizando <requestfocus></requestfocus>
luego también eliminar esta línea.
Pruébelo, creo que debería funcionar.