2011-10-17 13 views
24

Ahora, trato de ocultar el softkeyboard cuando tacto usuario fuera el teclado:cómo obtener la ventana de Actividad ¿Retornar sin vista?

((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)) 
.hideSoftInputFromWindow(editView.getWindowToken(), 
InputMethodManager.HIDE_NOT_ALWAYS); 

quiero poner la lógica en mi clase de actividad de base, por lo que si es posible getWindowToken sin View?

+1

¿Es esta una erratas "getgetWindowToken()"? – MKJParekh

+1

posible duplicado de [cómo ocultar el teclado virtual en Android después de hacer clic fuera de EditText?] (Http://stackoverflow.com/questions/4165414/how-to-hide-soft-keyboard-on-android- after-clicking-outside -edittext) – Reno

+1

'InputMethodManager InputManager = (InputMethodManager) getSystemService (Context.INPUT_METHOD_SERVICE);' ' inputManager.hideSoftInputFromWindow (findViewById (android.R.id.content) .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);' –

Respuesta

15

Sin duda, se puede utilizar:

getContentView().getWindowToken() 

o puede referirse a SO Quest

+9

resuelve de acuerdo Sugerencia de Hanry: findViewById (android.R.id.content) .getWindowToken() –

+1

No funciona. – technophyle

+6

No hay método getContentView(). Use findViewById (android.R.id.content) .getWindowToken() en su lugar. – mhsmith

25

me enfrentaba exactamente el mismo problema, mientras que la escritura OnPageChangeListener dentro de una actividad. Puedes usar una de estas soluciones. O bien:

getWindow().getDecorView().getRootView().getWindowToken() 

o:

findViewById(android.R.id.content).getWind‌​owToken() 
0
public static final String M_TOKEN = "mToken"; 

@Nullable 
protected IBinder getToken(Activity activity) { 
    try { 
     Field mTokenField = Activity.class.getDeclaredField(M_TOKEN); 
     mTokenField.setAccessible(true); 
     IBinder mToken = (IBinder) mTokenField.get(activity); 
     return mToken; 
    } catch (NoSuchFieldException e) { 
     // handle 
    } catch (IllegalAccessException e) { 
     // handle 
    } 
    return null; 
} 
0

Usted puede intentar esto en su etiqueta de actividad del archivo de manifiesto para ocultar el teclado.

android:windowSoftInputMode="stateHidden" 
1

Utilice simplemente getWindow().getDecorView().getWindowToken()

Cuestiones relacionadas