請嘗試以下邏輯來隱藏鍵盤自動打開。
嘗試將您的編輯文本置於單獨的線性佈局中並設置android:focusableInTouchMode="true"
。這將自動避免鍵盤自動打開。
<LinearLayout
android:id = "@+id/layout"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:focusable = "true"
android:focusableInTouchMode = "true">
<EditText
android:id = "@+id/edit_text"
android:layout_width = "match_content"
android:layout_height = "wrap_content"/>
</LinearLayout>
或者如果上述失敗,使用下面的代碼以編程方式隱藏。把它寫成一個單獨的函數並在代碼中調用它。
在創建視圖後,在您的片段中調用此方法,如下所示。
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
hideKeyboard();
}
public void hideKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) activity
.getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus()
.getWindowToken(), 0);
} // hideKeyboard
好運..!
do y ou把它修好了 –
可以請你發佈你的XML嗎? –