《每周一記》之Android軟鍵盤

為之于未有,治之于未亂。

在以往的項目開發中,關于軟鍵盤的知識點一直比較模糊,只是知道簡單的使用,當遇到問題的時候,也只能靠度娘或者蒙,剛好最近一個同事詢問相關問題時,才發現自己知識的薄弱,正好以此來激勵自己不斷學習。這也正是這邊文章的誕生的原因,更是讓我決定開始記錄《每周一記》。

參考:官方文檔

一、WindowSoftInputMode屬性

活動的主窗口如何與包含屏幕上的軟鍵盤窗口交互,這個屬性的設置將會影響兩件事情:

  • 軟鍵盤的狀態:當活動(Activity)成為用戶關注的焦點時,它是否隱藏或顯示。
  • 對活動主窗口進行的調整:無論是調整大小以便為軟鍵盤騰出空間,還是在軟鍵盤覆蓋窗口的一部分,以便當前焦點內容可見。

How the main window of the activity interacts with the window containing the on-screen soft keyboard. The setting for this attribute affects two things:

  • The state of the soft keyboard — whether it is hidden or visible — when the activity becomes the focus of user attention.
  • The adjustment made to the activity's main window — whether it is resized smaller to make room for the soft keyboard or whether its contents pan to make the current focus visible when part of the window is covered by the soft keyboard.
1、屬性詳解

The setting must be one of the values listed in the following table, or a combination of one "state..." value plus one "adjust..." value. Setting multiple values in either group — multiple "state..." values, for example — has undefined results. Individual values are separated by a vertical bar (|). For example:

<activity android:windowSoftInputMode="stateVisible|adjustResize" . . . >

此處設置的值(“stateUnspecified”和“adjustUnspecified”除外)將覆蓋主題中設置的值。

Values set here (other than "stateUnspecified" and "adjustUnspecified") override values set in the theme.

用來設置窗口軟鍵盤的交互模式,其屬性一共有9個取值,分別是:stateUnspecified,stateUnchanged,stateHidden,stateAlwaysHidden,stateVisible,stateAlwaysVisible,adjustUnspecified,adjustResize,adjustPan。

  • stateUnspecified
    默認交互方式,系統會根據界面采取相應的軟鍵盤的顯示模式。比如,當界面上只有輸入框和按鈕的時候,軟鍵盤就不會自動彈出,但是當有獲得焦點的輸入框的界面有滾動的需求的時候,會自動彈出軟鍵盤,例如外層為ScrollView。阻止鍵盤彈出的一個解決方案就是,在xml文件中,設置一個非輸入框控件獲取焦點。
    The state of the soft keyboard (whether it is hidden or visible) is not specified. The system will choose an appropriate state or rely on the setting in the theme.
    This is the default setting for the behavior of the soft keyboard.

  • stateUnchanged
    保持當前軟鍵盤狀態不變。舉個例子,假如當前界面鍵盤是隱藏的,那么跳轉之后的界面,軟鍵盤也是隱藏的;如果當前界面是顯示的,那么跳轉之后的界面,軟鍵盤也是顯示狀態。
    The soft keyboard is kept in whatever state it was last in, whether visible or hidden, when the activity comes to the fore.

  • stateHidden
    當用戶導航到而不是返回到該activity時,軟鍵盤總是被隱藏。
    The soft keyboard is hidden when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity.

  • stateAlwaysHidden
    當Activity的主窗口擁有輸入焦點時,軟鍵盤總是被隱藏。
    The soft keyboard is always hidden when the activity's main window has input focus.

  • stateVisible
    強制打開軟鍵盤。
    The soft keyboard is visible when that's normally appropriate (when the user is navigating forward to the activity's main window).

  • stateAlwaysVisible
    The soft keyboard is made visible when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity.

  • adjustUnspecified
    設置軟鍵盤與軟件的顯示內容之間的顯示關系,默認的設置模式。在這中情況下,系統會根據界面選擇不同的模式。如果界面里面有可以滾動的控件,比如ScrowView,系統會減小可以滾動的界面的大小,從而保證即使軟鍵盤顯示出來了,也能夠看到所有的內容。如果布局里面沒有滾動的控件,那么軟鍵盤可能就會蓋住一些內容。沒有滾動控件,軟鍵盤下面的布局都被遮擋住,若想修改,只能隱藏軟鍵盤,然后選擇。而且,重點注意一下上面的布局,當我們選擇的輸入框偏下的時候,上面的標題欄和布局被軟鍵盤頂上去了。
    It is unspecified whether the activity's main window resizes to make room for the soft keyboard, or whether the contents of the window pan to make the current focus visible on-screen. The system will automatically select one of these modes depending on whether the content of the window has any layout views that can scroll their contents. If there is such a view, the window will be resized, on the assumption that scrolling can make all of the window's contents visible within a smaller area.
    This is the default setting for the behavior of the main window.

  • adjustResize(壓縮模式)
    這個屬性表示Activity的主窗口總是會被調整大小,從而保證軟鍵盤顯示空間。
    The activity's main window is always resized to make room for the soft keyboard on screen.

  • adjustPan(平移模式)
    如果設置為這個屬性,那么Activity的屏幕大小并不會調整來保證軟鍵盤的空間,而是采取了另外一種策略,系統會通過布局的移動,來保證用戶要進行輸入的輸入框肯定在用戶的視野范圍內,從而讓用戶可以看到自己輸入的內容。對于沒有滾動控件的布局來說,這個其實就是默認的設置,如果我們選擇的位置偏下,上面的標題欄和部分控件會被頂上去。
    The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

備注:如果我們不設置"adjust..."的屬性,對于沒有滾動控件的布局來說,采用的是adjustPan方式,而對于有滾動控件的布局,則是采用的adjustResize方式。

2、使用方式
  • 代碼實現方式:
//在activity中的setContentView之前寫上以下代碼
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
  • xml實現方式:
//在 項目的AndroidManifest.xml文件中界面對應的<activity>里加入
android:windowSoftInputMode="adjustPan"

二、手動打開、關閉軟鍵盤

在開發過程中,難免會遇到想手動打開或者關閉軟鍵盤的情況,這時使用以下代碼不失為一種好辦法。

/**
 * 動態顯示隱藏軟鍵盤
 *
 * @param context context
 */
public static void toggleSoftInput(Context context) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}

三、軟鍵盤的Enter鍵

1、使用方式
  • 當layout中有多個EditText,把每個控件的android:singleLine的屬性都被設置成true的情況下,軟鍵盤的Enter鍵上 的文字會變成“Next”,按下后下個EditText會自動獲得焦點(實現了“Next”的功能);當最后一個控件獲得焦點的時候,Enter鍵上的文 字會變成“Done”,按下后軟鍵盤會自動隱藏起來。

  • 把EditText的ImeOptions屬性設置成不同的值,Enter鍵上可以顯示不同的文字或圖案
    actionNone : 回車鍵,按下后光標到下一行
    actionGo : Go,
    actionSearch : 一個放大鏡
    actionSend : Send
    actionNext : Next
    actionDone : Done,隱藏軟鍵盤,即使不是最后一個文本輸入框

inputView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                     //do something;              
                    return true;
                }
                return false;
            }
        });

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,237評論 6 537
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,957評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,248評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,356評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,081評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,485評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,534評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,720評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,263評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,025評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,204評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,787評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,461評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,874評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,105評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,945評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,205評論 2 375

推薦閱讀更多精彩內容