繁体   English   中英

为什么无法在Android Studio项目中隐藏软键盘,为什么?

[英]Can't hide soft keyboard in Android Studio project, why?

我正在尝试在Android Studio项目中隐藏软键盘。 hideKeyboard()方法中的View一直显示为null ...我不知道为什么...请告知。

  @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);
    }

}

您可以在android:windowSoftInputMode="stateHidden"使用android:windowSoftInputMode="stateHidden"在活动启动时隐藏键盘,如下所示

`<activity android:name=".YourActivity"
        android:windowSoftInputMode="stateHidden" />`

更新您的InputMethodManager:

InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

尝试:

imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

就我而言,它有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM