Android 軟鍵盤

來源:互聯網
上載者:User

標籤:

一些重要常量含義:

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 軟鍵盤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.