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