標籤:
一些重要常量含義:
HIDE_IMPLICIT_ONLY 常量值: 1 (0x00000001)
hideSoftInputFromWindow(IBinder, int)中的標誌,表示如果使用者未顯式地顯示軟鍵盤視窗,則隱藏視窗。
HIDE_NOT_ALWAYS 常量值: 2 (0x00000002)
hideSoftInputFromWindow(IBinder, int)中的標誌,表示軟鍵盤視窗總是隱藏,除非開始時以SHOW_FORCED顯示。
RESULT_HIDDEN 常量值: 3 (0x00000003)
showSoftInput(View, int, ResultReceiver)和hideSoftInputFromWindow(IBinder, int, ResultReceiver)中ResultReceiver結果代碼標誌:軟鍵盤視窗從顯示切換到隱藏時的狀態。
RESULT_SHOWN 常量值: 2 (0x00000002)
showSoftInput(View, int, ResultReceiver)和hideSoftInputFromWindow(IBinder, int, ResultReceiver)中ResultReceiver結果代碼標誌:軟鍵盤視窗從隱藏切換到顯示時的狀態。
RESULT_UNCHANGED_HIDDEN 常量值: 1 (0x00000001)
showSoftInput(View, int, ResultReceiver)和hideSoftInputFromWindow(IBinder, int, ResultReceiver)中ResultReceiver結果代碼標誌:軟鍵盤視窗不變保持隱藏時的狀態。
RESULT_UNCHANGED_SHOWN 常量值: 0 (0x00000000)
showSoftInput(View, int, ResultReceiver)和hideSoftInputFromWindow(IBinder, int, ResultReceiver)中ResultReceiver結果代碼標誌:軟鍵盤視窗不變保持顯示時的狀態。
SHOW_FORCED 常量值: 2 (0x00000002)
showSoftInput(View, int)標誌,表示使用者強制開啟IME(如長按菜單鍵),一直保持開啟直至只有顯式關閉。
SHOW_IMPLICIT 常量值: 1 (0x00000001)
showSoftInput(View, int)標誌,表示隱式顯示輸入視窗,非使用者直接要求。視窗可能不顯示。
android如何調用顯示和隱藏系統預設的IME
1.調用顯示系統預設的IME
方法一、
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); (這個方法可以實現IME在視窗上切換顯示,如果IME在視窗上已經顯示,則隱藏,如果隱藏,則顯示IME到視窗上)
2.調用隱藏系統預設的IME
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); (WidgetSearchActivity是當前的Activity)
3.擷取IME開啟的狀態
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);boolean isOpen=imm.isActive();isOpen若返回true,則表示IME開啟
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。
兩個方法的區別:
//顯示IME的軟鍵盤地區,這樣使用者可以到看到IME視窗並能與其互動。只能由當前啟用IME調用,因需令牌(token)驗證
showSoftInputFromInputMethod (IBinder token, int flags)
//本方法切換IME的視窗顯示。如輸入視窗已顯示,它隱藏。如無輸入視窗則顯示。
public void toggleSoftInputFromWindow (IBinder windowToken, int showFlags, int hideFlags)
特殊情況下軟鍵盤控制
1.TabActivity中嵌套的子Activity中調用軟鍵盤:
//顯示軟鍵盤
mReasonEt.requestFocus();InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(mReasonEt, 0);
//隱藏軟鍵盤
mReasonEt.requestFocus();InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromInputMethod(mReasonEt.getWindowToken(), 0);
在TabActivity中調用軟鍵盤,預設會把底部Tab菜單推上去,導致介面顯示和操作不方便,可以設定Activity的屬性,讓鍵盤直接覆蓋底部tab菜單;如下:
androidMainfest.xml檔案中在此Activity中寫入 android:windowSoftInputMode="adjustPan"
2.自訂Dialog中的EditText調用軟鍵盤:
//顯示軟鍵盤
myDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//隱藏軟鍵盤
myDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
3.捕獲螢幕點擊事件,隱藏IME
getWindow().getDecorView().setOnTouchListener(new OnTouchListener(){@Overridepublic boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)) .hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); return false; }});
Android 軟鍵盤