Android鍵盤顯示和隱藏,android鍵盤隱藏

來源:互聯網
上載者:User

Android鍵盤顯示和隱藏,android鍵盤隱藏

一、不自動彈出鍵盤:

帶有EditText控制項的在第一次顯示的時候會自動獲得focus,並彈出鍵盤,如果不想自動彈出鍵盤,有兩種方法:

方法一:在mainfest檔案中把對應的activity設定

android:windowSoftInputMode="stateHidden" 或者android:windowSoftInputMode="stateUnchanged"。

 

 

 

描述

"stateUnspecified"

軟鍵盤的狀態 (是否它是隱藏或可見 )沒有被指定。系統將選擇一個合適的狀態或依賴於主題的設定。

這個是為了軟體盤行為預設的設定。

"stateUnchanged"

軟鍵盤被保持無論它上次是什麼狀態,是否可見或隱藏,當主視窗出現在前面時。

"stateHidden"

當使用者選擇該 Activity時,軟鍵盤被隱藏——也就是,當使用者確定導航到該 Activity時,而不是返回到它由於離開另一個 Activity。

"stateAlwaysHidden"

軟鍵盤總是被隱藏的,當該 Activity主視窗擷取焦點時。

"stateVisible"

軟鍵盤是可見的,當那個是正常合適的時 (當使用者導航到 Activity主視窗時 )。

"stateAlwaysVisible"

當使用者選擇這個 Activity時,軟鍵盤是可見的——也就是,也就是,當使用者確定導航到該 Activity時,而不是返回到它由於離開另一個Activity。

"adjustUnspecified"

它不被指定是否該 Activity主 視窗調整大小以便留出軟鍵盤的空間,或是否視窗上的內容得到螢幕上當前的焦點是可見的。系統將自動選擇這些模式中一種主要依賴於是否視窗的內容有任何布局 視圖能夠滾動他們的內容。如果有這樣的一個視圖,這個視窗將調整大小,這樣的假設可以使滾動視窗的內容在一個較小的地區中可見的。這個是主視窗預設的行為 設定。

"adjustResize"

該 Activity主視窗總是被調整螢幕的大小以便留出軟鍵盤的空間

"adjustPan"

該 Activity主視窗並不調整螢幕的大小以便留出軟鍵盤的空間。相反,當前視窗的內容將自動移動以便當前焦點從不被鍵盤覆蓋和使用者能總是看到輸入內容的部分。這個通常是不期望比調整大小,因為使用者可能關閉軟鍵盤以便獲得與被覆蓋內容的互動操作。

 

這種方式編輯框還有游標。隱藏游標的方法:EditText.setCursorVisible(false)。

方法二:可以在布局中放一個隱藏的TextView,然後在onCreate的時候requsetFocus。

注意TextView不要設定Visiable=gone,否則會失效,可以在布局中放一個隱藏的TextView,然後在onCreate的時候requsetFocus。

<TextView

        android:id="@+id/text_notuse"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:focusable="true"

  android:focusableInTouchMode="true"/>

  TextView textView = (TextView)findViewById(R.id.text_notuse);

       textView.requestFocus();

二、手動顯示和隱藏系統鍵盤

1、方法一(如果IME在視窗上已經顯示,則隱藏,反之則顯示)。此方法使用尚不確定,不好使。

  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  

  imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);  

 

2、方法二(view為接受軟鍵盤輸入的視圖,SHOW_FORCED表示強制顯示)

  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  

  imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);  

   該方法在activity或者fragment的onCreate(或者onCreateView)函數中去調用,發現並不起作用,這是因為在onCreate中或者其他聲明周期函數(onStart,onResume等)中,該EditText還未被繪製出來,InputMethodManager還不能擷取到該控制項的焦點,所以鍵盤並不會顯示,而且manager.showSoftInput函數返回false,告訴你鍵盤並未顯示。所以只有當EditText完全被繪製出來了,才可以去擷取焦點。

解決方案:

edittext.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {  
      @Override  
      public void onGlobalLayout() {  
          InputMethodManager manager = (InputMethodManager) getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);  
          manager.showSoftInput(edittext, 0);  
      }  
});  

注意:這個監聽事件裡最好不要放toggleSoftInput方法,不然鍵盤會不停閃爍。

 3、強制隱藏鍵盤  

  imm.hideSoftInputFromWindow(view.getWindowToken(), 0);  

注意:如果第二個參數為HIDE_NOT_ALWAYS,那麼當showSoftInput的第二個參數為SHOW_FORCED時,則隱藏不掉。

4、調用隱藏系統預設的IME

  ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  (WidgetSearchActivity是當前的Activity)  

5、擷取IME開啟的狀態

  InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  

  boolean isOpen=imm.isActive();//isOpen若返回true,則表示IME開啟  

6、InputMethodManager的常量:

    public static final int HIDE_IMPLICIT_ONLY

 

           hideSoftInputFromWindow(IBinder, int)中的標誌,表示如果使用者未顯式地顯示軟鍵盤視窗,則隱藏視窗。

 

           常量值: 1 (0x00000001)

 

 

 

    public static final int HIDE_NOT_ALWAYS

 

    hideSoftInputFromWindow(IBinder, int)中的標誌,表示軟鍵盤視窗總是隱藏,除非開始時以SHOW_FORCED顯示。

 

    常量值: 2 (0x00000002)

 

 

 

    public static final int RESULT_HIDDEN

 

 

 

    showSoftInput(View, int, ResultReceiver)hideSoftInputFromWindow(IBinder, int, ResultReceiver)ResultReceiver結果代碼標誌:軟鍵盤視窗從顯示切換到隱藏時的狀態。

 

    常量值: 3 (0x00000003)

 

 

 

    public static final int RESULT_SHOWN

 

 

 

    showSoftInput(View, int, ResultReceiver)hideSoftInputFromWindow(IBinder, int, ResultReceiver)ResultReceiver結果代碼標誌:軟鍵盤視窗從隱藏切換到顯示時的狀態。

 

    常量值: 2 (0x00000002)

 

 

 

    public static final int RESULT_UNCHANGED_HIDDEN

 

 

 

    showSoftInput(View, int, ResultReceiver)hideSoftInputFromWindow(IBinder, int, ResultReceiver)ResultReceiver結果代碼標誌:軟鍵盤視窗不變保持隱藏時的狀態。

 

    常量值: 1 (0x00000001)

 

 

 

    public static final int RESULT_UNCHANGED_SHOWN

 

 

 

    showSoftInput(View, int, ResultReceiver)hideSoftInputFromWindow(IBinder, int, ResultReceiver)ResultReceiver結果代碼標誌:軟鍵盤視窗不變保持顯示時的狀態。

 

    常量值: 0 (0x00000000)

 

 

 

    public static final int SHOW_FORCED

 

    showSoftInput(View, int)標誌,表示使用者強制開啟IME(如長按菜單鍵),一直保持開啟直至只有顯式關閉。

 

    常量值: 2 (0x00000002)

 

 

 

    public static final int SHOW_IMPLICIT

 

      showSoftInput(View, int)標誌,表示隱式顯示輸入視窗,非使用者直接要求。視窗可能不顯示。

    常量值: 1 (0x00000001)

四、公用方法

    public void displayCompletions (View view, CompletionInfo[] completions)

    (譯者註:IME自動完成)

 

    public InputMethodSubtype getCurrentInputMethodSubtype ()

    (譯者註:擷取當前IME類型?)

 

    public List<InputMethodInfo> getEnabledInputMethodList ()

    (譯者註:擷取已啟用IME列表?)

 

    public List<InputMethodSubtype> getEnabledInputMethodSubtypeList (InputMethodInfo imi, boolean allowsImplicitlySelectedSubtypes)

 

    public List<InputMethodInfo> getInputMethodList ()

    (譯者註:擷取IME列表)

 

    public Map<InputMethodInfo, List<InputMethodSubtype>> getShortcutInputMethodsAndSubtypes ()

 

    public void hideSoftInputFromInputMethod (IBinder token, int flags)

    關閉/隱藏IME軟鍵盤地區,使用者不再看到或與其互動。只能由當前啟用IME調用,因需令牌(token)驗證。

    參數

    token       在IME啟動時提供令牌驗證,驗證後可對其進行操作。

    flags        提供額外的操作標誌。當前可以為0或 HIDE_IMPLICIT_ONLYHIDE_NOT_ALWAYS等位設定。

 

    public boolean hideSoftInputFromWindow (IBinder windowToken, int flags)

      hideSoftInputFromWindow(IBinder, int, ResultReceiver)的無傳回值版:從視窗上下文中確定當前接收輸入的視窗,隱藏其IME視窗

    參數

    windowToken 由視窗請求View.getWindowToken()返回得到的令牌(token)。

    flags        提供額外的操作標誌。當前可以為0或 HIDE_IMPLICIT_ONLY位設定。

 

    public boolean hideSoftInputFromWindow (IBinder windowToken, int flags, ResultReceiver resultReceiver)

    從視窗上下文中確定當前接收輸入的視窗,要求隱藏其軟鍵盤視窗。它可由使用者調用並得到結果而不僅僅是顯式要求IME視窗隱藏。

    參數

    windowToken 由視窗請求View.getWindowToken()返回得到的令牌(token)。

    flags        提供額外的操作標誌。當前可以為0或 HIDE_IMPLICIT_ONLY位設定。

        resultReceiver  如不為空白,當IME處理請求告訴你完成時調用。你收到的結果碼可以是RESULT_UNCHANGED_SHOWNRESULT_UNCHANGED_HIDDENRESULT_SHOWN,或RESULT_HIDDEN

 

    public void hideStatusIcon (IBinder imeToken)

    (譯者註:隱藏狀態列表徵圖?)

 

    public boolean isAcceptingText ()

    當前服務視圖接受全文編輯返回真。沒有IME聯結為false,這時其只能處理原始按鍵事件。

 

    public boolean isActive (View view)

    視圖為當前輸入的啟用視圖時返回真。

 

    public boolean isActive ()

    IME中的任意視圖啟用時返回真。

 

    public boolean isFullscreenMode ()

    判斷相關IME是否以全螢幕模式運行。全屏時,完全覆蓋你的UI時,返回真,否則返回假。

 

    public boolean isWatchingCursor (View view)

    如當前IME要看到輸入編輯者的游標位置時返回真。

 

    public void restartInput (View view)

    如有IME聯結至視圖,重啟輸入以顯示新的內容。可在以下情況時調用此方法:視圖的文字導致IME外觀變化或有按鍵輸入資料流,如應用程式調用TextView.setText()時。

             參數

    view        文字發生變化的視圖。

 

    public void sendAppPrivateCommand (View view, String action, Bundle data)

    對當前IME調用 InputMethodSession.appPrivateCommand()

             參數

    view         可選的發送命令的視圖,如你要發送命令而不考慮視圖附加到IME,此項可以為空白。

    action      執行的命令名稱。必須是範圍的名稱,如首碼包名稱,這樣不同的開發人員就不會建立衝突的命令。

    data         命令中包含的任何資料。

 

    public boolean setCurrentInputMethodSubtype (InputMethodSubtype subtype)

    (譯者註:此方法為3.0中新增的方法)

 

    public void setInputMethod (IBinder token, String id)

    強制切換到新IME組件。只能由持有token的應用程式(application)或服務(service) 調用當前啟用IME。

             參數

    token       在IME啟動時提供令牌驗證,驗證後可對其進行操作。

    id              切換到新IME的唯一標識。

 

    public void setInputMethodAndSubtype (IBinder token, String id, InputMethodSubtype subtype)

    強制切換到一個新的IME和指定的類型。只能由持有token的應用程式(application)或服務(service) 調用當前啟用IME。(譯者註:此方法為3.0中新增的方法)

             參數

    token       在IME啟動時提供令牌驗證,驗證後可對其進行操作。

    id              切換到新IME的唯一標識。

    subtype 切換到新IME的新類型。

 

    public void showInputMethodAndSubtypeEnabler (String topId)

    (譯者註:此方法為3.0中新增的方法)

 

    public void showInputMethodPicker ()

    (譯者註:顯示IME菜單列表)

 

    public boolean showSoftInput (View view, int flags, ResultReceiver resultReceiver)

    如需要,顯式要求當前IME的軟鍵盤地區向使用者顯示。當使用者與視圖互動,使用者表示要開始執行輸入操作時,可以調用此方法。

             參數

    view         當前焦點視圖,可接受軟鍵盤輸入。

    flags        提供額外的操作標誌。當前可以是0或SHOW_IMPLICIT 位設定。

    resultReceiver         如不為空白,當IME處理請求告訴你完成時調用。你收到的結果碼可以是RESULT_UNCHANGED_SHOWNRESULT_UNCHANGED_HIDDEN,RESULT_SHOWN, 或 RESULT_HIDDEN 。

 

    public boolean showSoftInput (View view, int flags)

showSoftInput(View, int, ResultReceiver)的無傳回值版:如需要,顯式要求當前IME的軟鍵盤地區向使用者顯示。

             參數

    view         當前焦點視圖,可接受軟鍵盤輸入。

    flags        提供額外的操作標誌。當前可以是0或SHOW_IMPLICIT 位設定。

 

    public void showSoftInputFromInputMethod (IBinder token, int flags)

    顯示IME的軟鍵盤地區,這樣使用者可以到看到IME視窗並能與其互動。只能由當前啟用IME調用,因需令牌(token)驗證。

             參數

    token       在IME啟動時提供令牌驗證,驗證後可對其進行操作。

    flags        提供額外的操作標誌。可以是0或 SHOW_IMPLICITSHOW_FORCED位設定。

 

    public void showStatusIcon (IBinder imeToken, String packageName, int iconId)

    (譯者註:顯示狀態列表徵圖?)

 

    public boolean switchToLastInputMethod (IBinder imeToken)

 

    public void toggleSoftInput (int showFlags, int hideFlags)

    (譯者註:切換軟鍵盤)

 

    public void toggleSoftInputFromWindow (IBinder windowToken, int showFlags, int hideFlags)

    本方法切換IME的視窗顯示。如輸入視窗已顯示,它隱藏。如無輸入視窗則顯示。

             參數

    windowToken  由視窗請求View.getWindowToken()返回得到的令牌(token)。

    showFlags       提供額外的操作標誌。當前可以為0或 HIDE_IMPLICIT_ONLY位設定。

    hideFlags         提供額外的操作標誌。可以是0或 HIDE_IMPLICIT_ONLYHIDE_NOT_ALWAYS位設定。

 

    public void updateCursor (View view, int left, int top, int right, int bottom)

    返回視窗的當前游標位置。

 

    public void updateExtractedText (View view, int token, ExtractedText text)

    (譯者註:當內容變化時文字編輯器調用此方法,通知其新提取文本。)

 

    public void updateSelection (View view, int selStart, int selEnd, int candidatesStart, int candidatesEnd)

返回當前選擇地區。 鳴謝:  http://blog.csdn.net/h7870181/article/details/8332991  http://blog.csdn.net/pi9nc/article/details/9196779  http://blog.csdn.net/lxlmycsdnfree/article/details/60962731

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.