【Android】鍵盤的展開和收起


鍵盤的展開和收起主要使用到類InputMethodManager:http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html

其大致方法如下:

1 public void hide_keyboard_from(Context context, View view) {
2         InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
3         inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
4     }
5     
6 public void show_keyboard_from(Context context, View view) {
7         InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
8         inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
9     }

在展開和收起方法調用的時候,都要求傳入第二個參數。大致有以下四種:

1)HIDE_IMPLICIT_ONLY:indicate that the soft input window should only be hidden if it was not explicitly shown by the user.

2)HIDE_NOT_ALWAYS:to indicate that the soft input window should normally be hidden, unless it was originally shown with SHOW_FORCED

3)SHOW_FORCED:indicate that the user has forced the input method open (such as by long-pressing menu) so it should not be closed until they explicitly do so.

4)SHOW_IMPLICIT:indicate that this is an implicit request to show the input window, not as the result of a direct request by the user.

1)和2)是用於收起鍵盤的,3)和4)是用於展開鍵盤的。

 

如果用戶是點擊輸入框彈出的鍵盤,則調用1)是無法隱藏的,系統此時判斷使用戶有意呼喚鍵盤的!調用2)則可以收起鍵盤;

如果用戶是使用3)作為參數顯示鍵盤的,則1)和2)都是無法隱藏鍵盤的,此時需要用參數0,強制隱藏一切鍵盤;

如果用戶是使用4)作為參數顯示鍵盤的,則1)和2)都是可以隱藏鍵盤的。

 

總結起來就是:Forced > explicit > implicit。

對於隱藏鍵盤,其中1)是屬於implicit,2)是屬於explicit,0則是屬於force;

對於顯示鍵盤,則4)是屬於implicit,輸入框呼出屬於explicit,3)則是屬於force;

只有隱藏的級別>=展開的級別,才能將鍵盤隱藏掉。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM