Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 系統預設輸入法的顯示與隱藏控制

編輯:關於 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,則表示輸入法打開

補充一:隱藏軟鍵盤

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

補充二:顯示軟鍵盤(控制項 ID 可以是 EditText、TextView 等)

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

補充三:不讓鍵盤自動彈出

帶有 EditText 的畫面在首次顯示時,往往會自動取得焦點並彈出鍵盤。如果不想讓它自動彈出,有兩種做法:

方法一:在 manifest 檔案中為對應的 Activity 設定

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

方法二:在佈局中放一個隱藏的 TextView,並在 onCreate 時呼叫 requestFocus。

要注意的是,這個 TextView 不要設為 Visible=gone,否則會失效。也就是在佈局中放一個隱藏的 TextView,然後在 onCreate 時 requestFocus。

 <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. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved