2011-10-17 30 views
24

は今、私はキーボードの外softkeyboardたときに、ユーザーのタッチを隠そう:アクティビティのwindowTokenを表示せずに取得するには?

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

私はそれを見ることなく、getWindowTokenすることが可能であるので、もし、私の基本アクティビティクラスにロジックを入れたいですか?

+1

を使用し、これはタイプミス "getgetWindowToken()" とは? – MKJParekh

+1

[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);' –

答えて

15

きっとあなたは使用することができます。

getContentView().getWindowToken() 

たり、活動の中OnPageChangeListenerを書きながら、私は、まったく同じ問題に直面したSO Quest

+9

を解消によるHanryの提案:findViewById(android.R.id.content).getWindowToken() –

+1

これは動作しません。 – technophyle

+6

getContentView()メソッドはありません。代わりにfindViewById(android.R.id.content).getWindowToken()を使用してください。 – mhsmith

25

を参照することができます。これらのソリューションのいずれかを使用できます。次のいずれかの

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

か:

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

あなたはキーボードを非表示にするマニフェストファイルのアクティビティタグでこれを試すことができます。

android:windowSoftInputMode="stateHidden" 
1

単にgetWindow().getDecorView().getWindowToken()

関連する問題