现在,我尝试在用户触摸键盘之外时隐藏软键盘:
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(editView.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
我想把逻辑放在我的基础活动类中,所以有没有可能在没有View的情况下获取getWindowToken?
现在,我尝试在用户触摸键盘之外时隐藏软键盘:
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(editView.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
我想把逻辑放在我的基础活动类中,所以有没有可能在没有View的情况下获取getWindowToken?
我在编写一个活动中的OnPageChangeListener
时遇到了完全相同的问题。您可以使用以下其中一种解决方案:
getWindow().getDecorView().getRootView().getWindowToken()
或:findViewById(android.R.id.content).getWindowToken()
getWindow().getDecorView().getWindowToken()
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;
}
WindowManager.LayoutParams
中获取令牌。getWindow().getAttributes().token
在Kotlin中:
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(window.attributes.token, 0)
imm.hideSoftInputFromWindow(view.windowToken, 0)
android:windowSoftInputMode="stateHidden"