Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android InputMethodManager輸入法簡介

Android InputMethodManager輸入法簡介

編輯:關於Android編程

一、結構

public final class InputMethodManager extends Object

Java.lang.Object

android.view.inputmethod.InputMethodManager

二、類概述

整個輸入法框架(IMF)結構的核心API,應用程序之間進行調度和當前輸入法交互。你可以用Context.getSystemService()取得這一接口的實例。

架構總述(Architecture Overview)

輸入法框架(IMF)共有三個主要部分:

* 輸入法管理器,管理各部分的交互。它是一個客戶端API,存在於各個應用程序的context中,用來溝通管理所有進程間交互的全局系統服務。

* 輸入法(IME) ,實現一個允許用戶生成文本的獨立交互模塊。系統綁定一個當前的輸入法。使其創建和生成,決定輸入法何時隱藏或者顯示它的UI。同一時間只能有一個IME運行。

* 客戶應用程序,通過輸入法管理器控制輸入焦點和IME的狀態。一次只能有一個客戶端使用IME。

應用程序(Applications)

大多數情況下,使用標准TextView或其子類的應用程序只要做少量工作就可以讓軟鍵盤(soft input methods)正常工作。你要注意的是:

* 正確設置你的可編輯文本視圖inputType,這樣輸入法有足夠的上下文幫助用戶向其輸入文本。

* 當輸入法顯示時妥善處理隱藏屏幕空間。理想的情況下應用程序應處理窗口變小,如必要你可以利用系統執行平移窗口。你應在活動(activity)中設置windowSoftInputMode屬性或調整創建窗口的相應值,以便系統決定平移或縮放(它會嘗試自動調整但可能出錯)。

* 你還可以通過相同的windowSoftInputMode控制窗口的首選軟鍵盤狀態(打開、關閉等)。

通過API可以與IMF及其IME交互實現更細致的控制,如顯示或輸入區域,用戶選取某輸入法等。

當你編寫自己的文本編輯器的時候,你要實現onCreateInputConnection(EditorInfo)以返回你的InputConnection的接口實例,用來允許IME和你的文本編輯域來交互。

  輸入法(Input Methods)

一個輸入法(IME)是Service(服務)的實現,通常繼承自 InputMethodService。IME提供核心的 InputMethod接口,盡管提供InputMethod通常是由InputMethodService來處理,而IME的實現只需要處理更高層的API。

更多信息參閱InputMethodService。

  安全(Security)

輸入法關系到許多安全問題,因其基本不受約束地驅動UI並監視用戶輸入。Android輸入法框架還允許調度第三方IME,所必須小心以限制他們的選擇和相互作用。

以下是IMF背後安全架構的要點:

* 只允許系統訪問經BIND_INPUT_METHOD權限許可訪問IME的InputMethod接口。通過綁定到要求這個權限的服務來強制實現這一點。所以系統可以保證沒有不被信任的客戶端在它的控制之外訪問到當前的輸入法。

* IMF中可能有許多客戶進程,但在同一時間只有一個是激活的。未激活客戶端不能與IMF核心交互通過下述機制實現。

* 輸入法客戶端只可訪問InputMethodSession接口。每個客戶端創建一個接口實例,只有與激活客戶相關聯的會話的調用才會被IME處理。這點通過普通IME的 AbstractInputMethodService執行,必須由IME顯式的處理,而IME正是InputMethodSession的自定義實現。

* 只有激活的InputConnection接受操作。IMF通知每個客戶進程是否激活,IMF忽略非激活進程對當前輸入聯接的調用。這確保了當前IME只將事件和文本編輯交付用戶可見焦點的UI。

* 一個IME永遠不能在屏幕關閉時與 InputConnection交互。這是通過當屏幕關閉時所有客戶端無效,並防止不良IME在用戶無法關注其行為時驅動UI。

* 客戶應用程序可以要求系統選取一個新的IME,但不能編程式選擇之一。這是避免惡意程序在用戶導航到其它應用程序時,選擇自己的IME並保持運行。也就是說,IME可以編程式的要求系統選擇其它IME,因它完全控制了用戶輸入。

  用戶在可切換至一個新的IME前必須顯式的啟用它,以確認系統了解它,使其處於可用狀態。

三、常量

  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)標志,表示用戶強制打開輸入法(如長按菜單鍵),一直保持打開直至只有顯式關閉。

  常量值: 2 (0x00000002)

  public static final int SHOW_IMPLICIT

    showSoftInput(View, int)標志,表示隱式顯示輸入窗口,非用戶直接要求。窗口可能不顯示。

  常量值: 1 (0x00000001)

四、公共方法

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

  (譯者注:輸入法自動完成)

  public InputMethodSubtype getCurrentInputMethodSubtype ()

  (譯者注:獲取當前輸入法類型?)

  public List<InputMethodInfo> getEnabledInputMethodList ()

  (譯者注:獲取已啟用輸入法列表?)

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

  public List<InputMethodInfo> getInputMethodList ()

  (譯者注:獲取輸入法列表)

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

  public void hideSoftInputFromInputMethod (IBinder token, int flags)

  關閉/隱藏輸入法軟鍵盤區域,用戶不再看到或與其交互。只能由當前激活輸入法調用,因需令牌(token)驗證。

  參數

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

  flags 提供額外的操作標志。當前可以為0或 HIDE_IMPLICIT_ONLY, HIDE_NOT_ALWAYS等位設置。

  public boolean hideSoftInputFromWindow (IBinder windowToken, int flags)

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

  參數

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

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

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

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

  參數

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

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

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

  public void hideStatusIcon (IBinder imeToken)

  (譯者注:隱藏狀態欄圖標?)

  public boolean isAcceptingText ()

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

  public boolean isActive (View view)

  視圖為當前輸入的激活視圖時返回真。

  public boolean isActive ()

  輸入法中的任意視圖激活時返回真。

  public boolean isFullscreenMode ()

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

  public boolean isWatchingCursor (View view)

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

  public void restartInput (View view)

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

 參數

  view 文字發生變化的視圖。

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

  對當前輸入法調用 InputMethodSession.appPrivateCommand()。

參數

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

  action 執行的命令名稱。必須是作用域的名稱,如前綴包名稱,這樣不同的開發者就不會創建沖突的命令。

  data 命令中包含的任何數據。

  public boolean setCurrentInputMethodSubtype (InputMethodSubtype subtype)

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

  public void setInputMethod (IBinder token, String id)

  強制切換到新輸入法部件。只能由持有token的應用程序(application)或服務(service) 調用當前激活輸入法。

 參數

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

  id 切換到新輸入法的唯一標識。
  public void setInputMethodAndSubtype (IBinder token, String id, InputMethodSubtype subtype)

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

 參數

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

  id 切換到新輸入法的唯一標識。

  subtype 切換到新輸入法的新類型。

  public void showInputMethodAndSubtypeEnabler (String topId)

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

  public void showInputMethodPicker ()

  (譯者注:顯示輸入法菜單列表)

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

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

參數

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

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

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

  public boolean showSoftInput (View view, int flags)

  showSoftInput(View, int, ResultReceiver)的無返回值版:如需要,顯式要求當前輸入法的軟鍵盤區域向用戶顯示。

參數

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

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

  public void showSoftInputFromInputMethod (IBinder token, int flags)

  顯示輸入法的軟鍵盤區域,這樣用戶可以到看到輸入法窗口並能與其交互。只能由當前激活輸入法調用,因需令牌(token)驗證。

參數

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

  flags 提供額外的操作標志。可以是0或 SHOW_IMPLICIT, SHOW_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)

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

參數

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

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

  hideFlags 提供額外的操作標志。可以是0或 HIDE_IMPLICIT_ONLY, HIDE_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)

  返回當前選擇區域。

android如何調用顯示和隱藏系統默認的輸入法

1.調用顯示系統默認的輸入法

方法一、

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

imm.showSoftInput(m_receiverView(接受軟鍵盤輸入的視圖(View)),InputMethodManager.SHOW_FORCED(提供當前操作的標記,SHOW_FORCED表示強制顯示));

方法二、

InputMethodManager m=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); (這個方法可以實現輸入法在窗口上切換顯示,如果輸入法在窗口上已經顯示,則隱藏,如果隱藏,則顯示輸入法到窗口上)

2.調用隱藏系統默認的輸入法

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

3.獲取輸入法打開的狀態

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();
isOpen若返回true,則表示輸入法打開

1、//隱藏軟鍵盤  

((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  

2、//顯示軟鍵盤,控件ID可以是EditText,TextView  

((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(控件ID, 0); 

3、不自動彈出鍵盤:

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

方法一:在mainfest文件中把對應的activity設置

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

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

注意TextView不要設置Visiable=gone,否則會失效

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

注意TextView不要設置Visiable=gone,否則會失效

<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();

Android軟鍵盤學習InputMethodManager

當我們在Android提供的EditText中單擊的時候,會自動的彈出軟鍵盤,其實對於軟鍵盤的控制我們可以通過InputMethodManager這個類來實現。我們需要控制軟鍵盤的方式就是兩種一個是像EditText那樣當發生onClick事件的時候出現軟鍵盤,還有就是當打開某個程序的時候自動的彈出軟鍵盤。

public class InputMethodManagerTest extends Activity implements OnClickListener{ 
  private Button button; 
   
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    LinearLayout layout=new LinearLayout(this); 
    LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); 
    button=new Button(this); 
    button.setId(123); 
    button.setText("Hello GaoMatrix"); 
    button.setOnClickListener(this); 
    layout.addView(button, layoutParams); 
    setContentView(layout); 
     
    /** 
     * 用一個定時器控制當打開這個Activity的時候就出現軟鍵盤 
     */ 
    Timer timer=new Timer(); 
    timer.schedule(new TimerTask() { 
      @Override 
      public void run() { 
        InputMethodManager inputMethodManager=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
        inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); 
      } 
    }, 2000); 
  } 
  /** 
   * 當單擊事件的時候觸發顯示軟鍵盤 
   */ 
  @Override 
  public void onClick(View v) { 
    InputMethodManager inputMethodManager=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
    inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); 
  } 
} 

這個InputMethodManager類裡面的toggleSoftInput方法的API中的解釋是:

This method toggles the input method window display. If the input window is already displayed, it gets hidden. If not the input window will be displayed.

這個方法在界面上切換輸入法的功能,如果輸入法出於現實狀態,就將他隱藏,如果處於隱藏狀態,就顯示輸入法。

而對於第二中方式進入Activity就自動顯示軟鍵盤,在一個定時器中,也就是在一個線程中執行,只不過是延遲2秒執行,原因是在onCreate函數中Android程序未將屏幕繪制完成。

以上就是本文的全部內容,希望能給大家一個參考,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved