无法在Android Studio项目中隐藏软键盘,为什么?
我试图在Android Studio项目中隐藏软键盘。hideKeyboard方法中的视图一直显示为null。。。我不明白为什么。。。请告知无法在Android Studio项目中隐藏软键盘,为什么?,android,view,android-softkeyboard,Android,View,Android Softkeyboard,我试图在Android Studio项目中隐藏软键盘。hideKeyboard方法中的视图一直显示为null。。。我不明白为什么。。。请告知 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_rewashlog_options); luxuryCo
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rewashlog_options);
luxuryCountTextView = (TextView) findViewById(R.id.luxuryCountTextView);
fullCountTextView = (TextView) findViewById(R.id.fullCountTextView);
quickCountTextView = (TextView) findViewById(R.id.quickCountTextView);
totalCountTextView = (TextView) findViewById(R.id.totalCountTextView);
emailRecipient = (EditText) findViewById(R.id.emailRecipient);
instantiateMonthSpinner();
instantiateYearSpinner();
getEntireRewashList();
updateListView();
hideKeyboard();
}
private void hideKeyboard() {
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
更新您的InputMethodManager:
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
您可以在mainfest中使用android:windowSoftInputMode=stateHidden在活动启动时隐藏键盘,如下所示
`<activity android:name=".YourActivity"
android:windowSoftInputMode="stateHidden" />`
尝试:
在我的情况下,它可以工作。尝试使用handler.post隐藏它。您现在正试图在显示之前将其隐藏。请尝试使用yourEditText.getWindowToken而不是view.getWindowToken。我想这是电子邮件收件人。
imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);