《每周一記》之Android軟鍵盤(pán)

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

在以往的項(xiàng)目開(kāi)發(fā)中,關(guān)于軟鍵盤(pán)的知識(shí)點(diǎn)一直比較模糊,只是知道簡(jiǎn)單的使用,當(dāng)遇到問(wèn)題的時(shí)候,也只能靠度娘或者蒙,剛好最近一個(gè)同事詢問(wèn)相關(guān)問(wèn)題時(shí),才發(fā)現(xiàn)自己知識(shí)的薄弱,正好以此來(lái)激勵(lì)自己不斷學(xué)習(xí)。這也正是這邊文章的誕生的原因,更是讓我決定開(kāi)始記錄《每周一記》。

參考:官方文檔

一、WindowSoftInputMode屬性

活動(dòng)的主窗口如何與包含屏幕上的軟鍵盤(pán)窗口交互,這個(gè)屬性的設(shè)置將會(huì)影響兩件事情:

  • 軟鍵盤(pán)的狀態(tài):當(dāng)活動(dòng)(Activity)成為用戶關(guān)注的焦點(diǎn)時(shí),它是否隱藏或顯示。
  • 對(duì)活動(dòng)主窗口進(jìn)行的調(diào)整:無(wú)論是調(diào)整大小以便為軟鍵盤(pán)騰出空間,還是在軟鍵盤(pán)覆蓋窗口的一部分,以便當(dāng)前焦點(diǎn)內(nèi)容可見(jiàn)。

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" . . . >

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

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

用來(lái)設(shè)置窗口軟鍵盤(pán)的交互模式,其屬性一共有9個(gè)取值,分別是:stateUnspecified,stateUnchanged,stateHidden,stateAlwaysHidden,stateVisible,stateAlwaysVisible,adjustUnspecified,adjustResize,adjustPan。

  • stateUnspecified
    默認(rèn)交互方式,系統(tǒng)會(huì)根據(jù)界面采取相應(yīng)的軟鍵盤(pán)的顯示模式。比如,當(dāng)界面上只有輸入框和按鈕的時(shí)候,軟鍵盤(pán)就不會(huì)自動(dòng)彈出,但是當(dāng)有獲得焦點(diǎn)的輸入框的界面有滾動(dòng)的需求的時(shí)候,會(huì)自動(dòng)彈出軟鍵盤(pán),例如外層為ScrollView。阻止鍵盤(pán)彈出的一個(gè)解決方案就是,在xml文件中,設(shè)置一個(gè)非輸入框控件獲取焦點(diǎn)。
    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
    保持當(dāng)前軟鍵盤(pán)狀態(tài)不變。舉個(gè)例子,假如當(dāng)前界面鍵盤(pán)是隱藏的,那么跳轉(zhuǎn)之后的界面,軟鍵盤(pán)也是隱藏的;如果當(dāng)前界面是顯示的,那么跳轉(zhuǎn)之后的界面,軟鍵盤(pán)也是顯示狀態(tài)。
    The soft keyboard is kept in whatever state it was last in, whether visible or hidden, when the activity comes to the fore.

  • stateHidden
    當(dāng)用戶導(dǎo)航到而不是返回到該activity時(shí),軟鍵盤(pán)總是被隱藏。
    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
    當(dāng)Activity的主窗口擁有輸入焦點(diǎn)時(shí),軟鍵盤(pán)總是被隱藏。
    The soft keyboard is always hidden when the activity's main window has input focus.

  • stateVisible
    強(qiáng)制打開(kāi)軟鍵盤(pán)。
    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
    設(shè)置軟鍵盤(pán)與軟件的顯示內(nèi)容之間的顯示關(guān)系,默認(rèn)的設(shè)置模式。在這中情況下,系統(tǒng)會(huì)根據(jù)界面選擇不同的模式。如果界面里面有可以滾動(dòng)的控件,比如ScrowView,系統(tǒng)會(huì)減小可以滾動(dòng)的界面的大小,從而保證即使軟鍵盤(pán)顯示出來(lái)了,也能夠看到所有的內(nèi)容。如果布局里面沒(méi)有滾動(dòng)的控件,那么軟鍵盤(pán)可能就會(huì)蓋住一些內(nèi)容。沒(méi)有滾動(dòng)控件,軟鍵盤(pán)下面的布局都被遮擋住,若想修改,只能隱藏軟鍵盤(pán),然后選擇。而且,重點(diǎn)注意一下上面的布局,當(dāng)我們選擇的輸入框偏下的時(shí)候,上面的標(biāo)題欄和布局被軟鍵盤(pán)頂上去了。
    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(壓縮模式)
    這個(gè)屬性表示Activity的主窗口總是會(huì)被調(diào)整大小,從而保證軟鍵盤(pán)顯示空間。
    The activity's main window is always resized to make room for the soft keyboard on screen.

  • adjustPan(平移模式)
    如果設(shè)置為這個(gè)屬性,那么Activity的屏幕大小并不會(huì)調(diào)整來(lái)保證軟鍵盤(pán)的空間,而是采取了另外一種策略,系統(tǒng)會(huì)通過(guò)布局的移動(dòng),來(lái)保證用戶要進(jìn)行輸入的輸入框肯定在用戶的視野范圍內(nèi),從而讓用戶可以看到自己輸入的內(nèi)容。對(duì)于沒(méi)有滾動(dòng)控件的布局來(lái)說(shuō),這個(gè)其實(shí)就是默認(rèn)的設(shè)置,如果我們選擇的位置偏下,上面的標(biāo)題欄和部分控件會(huì)被頂上去。
    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.

備注:如果我們不設(shè)置"adjust..."的屬性,對(duì)于沒(méi)有滾動(dòng)控件的布局來(lái)說(shuō),采用的是adjustPan方式,而對(duì)于有滾動(dòng)控件的布局,則是采用的adjustResize方式。

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

二、手動(dòng)打開(kāi)、關(guān)閉軟鍵盤(pán)

在開(kāi)發(fā)過(guò)程中,難免會(huì)遇到想手動(dòng)打開(kāi)或者關(guān)閉軟鍵盤(pán)的情況,這時(shí)使用以下代碼不失為一種好辦法。

/**
 * 動(dòng)態(tài)顯示隱藏軟鍵盤(pán)
 *
 * @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);
}

三、軟鍵盤(pán)的Enter鍵

1、使用方式
  • 當(dāng)layout中有多個(gè)EditText,把每個(gè)控件的android:singleLine的屬性都被設(shè)置成true的情況下,軟鍵盤(pán)的Enter鍵上 的文字會(huì)變成“Next”,按下后下個(gè)EditText會(huì)自動(dòng)獲得焦點(diǎn)(實(shí)現(xiàn)了“Next”的功能);當(dāng)最后一個(gè)控件獲得焦點(diǎn)的時(shí)候,Enter鍵上的文 字會(huì)變成“Done”,按下后軟鍵盤(pán)會(huì)自動(dòng)隱藏起來(lái)。

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

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容