那么,彈出軟鍵盤后必然會(huì)造成原有布局高度的減少,那么系統(tǒng)應(yīng)該如何來處理布局的減少?我們能否在應(yīng)用程序中進(jìn)行自定義的控制?這些是本文要討論的重點(diǎn)。
一、軟鍵盤顯示的原理
軟件盤的本質(zhì)是什么?軟鍵盤其實(shí)是一個(gè)Dialog!
InputMethodService為我們的輸入法創(chuàng)建了一個(gè)Dialog,并且將該Dialog的Window的某些參數(shù)(如Gravity)進(jìn)行了設(shè)置,使之能夠在底部或者全屏顯示。當(dāng)我們點(diǎn)擊輸入框時(shí),系統(tǒng)對(duì)活動(dòng)主窗口進(jìn)行調(diào)整,從而為輸入法騰出相應(yīng)的空間,然后將該Dialog顯示在底部,或者全屏顯示。
二、活動(dòng)主窗口調(diào)整
android定義了一個(gè)屬性,名字為windowSoftInputMode, 用它可以讓程序可以控制活動(dòng)主窗口調(diào)整的方式。我們可以在AndroidManifet.xml中對(duì)Activity進(jìn)行設(shè)置。如:android:windowSoftInputMode="stateUnchanged|adjustPan"
該屬性可選的值有兩部分,一部分為軟鍵盤的狀態(tài)控制,另一部分是活動(dòng)主窗口的調(diào)整。前一部分本文不做討論,請(qǐng)讀者自行查閱android文檔。
模式一,壓縮模式
windowSoftInputMode的值如果設(shè)置為adjustResize,那么該Activity主窗口總是被調(diào)整大小以便留出軟鍵盤的空間。
我們通過一段代碼來測(cè)試一下,當(dāng)我們?cè)O(shè)置了該屬性后,彈出輸入法時(shí),系統(tǒng)做了什么。
重寫Layout布局:
- public class ResizeLayout extends LinearLayout{
- private static int count = 0 ;
- public ResizeLayout(Context context, AttributeSet attrs) {
- super (context, attrs);
- }
- @Override
- protected void onSizeChanged( int w, int h, int oldw, int oldh) {
- super .onSizeChanged(w, h, oldw, oldh);
- Log.e( "onSizeChanged " + count++, "=>onResize called! w=" +w + ",h=" +h+ ",oldw=" +oldw+ ",oldh=" +oldh);
- }
- @Override
- protected void onLayout( boolean changed, int l, int t, int r, int b) {
- super .onLayout(changed, l, t, r, b);
- Log.e( "onLayout " + count++, "=>OnLayout called! l=" + l + ", t=" + t + ",r=" + r + ",b=" +b);
- }
- @Override
- protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) {
- super .onMeasure(widthMeasureSpec, heightMeasureSpec);
- Log.e( "onMeasure " + count++, "=>onMeasure called! widthMeasureSpec=" + widthMeasureSpec + ", heightMeasureSpec=" + heightMeasureSpec);
- }
我們的布局設(shè)置為:
- < com.winuxxan.inputMethodTest.ResizeLayout
- xmlns:android = "http://schemas.android.com/apk/res/android"
- android:id = "@+id/root_layout"
- android:layout_width = "fill_parent"
- android:layout_height = "fill_parent"
- android:orientation = "vertical"
- >
- < EditText
- android:layout_width = "fill_parent"
- android:layout_height = "wrap_content"
- />
- < LinearLayout
- android:id = "@+id/bottom_layout"
- android:layout_width = "fill_parent"
- android:layout_height = "fill_parent"
- android:orientation = "vertical"
- android:gravity = "bottom" > s
- < TextView
- android:layout_width = "fill_parent"
- android:layout_height = "wrap_content"
- android:text = "@string/hello"
- android:background = "#77777777"
- />
- </ LinearLayout >
- </ com.winuxxan.inputMethodTest.ResizeLayout >
AndroidManifest.xml的Activity設(shè)置屬性:android:windowSoftInputMode = "adjustResize"
運(yùn)行程序,點(diǎn)擊文本框,查看調(diào)試信息:
E/onMeasure 6(7960): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec = 1073742024
E/onMeasure 7(7960): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec = 1073742025
E/onSizeChanged 8(7960): =>onSizeChanged called! w=320,h=201,oldw=320,oldh=377
E/onLayout 9(7960): =>OnLayout called! l=0, t=0,r=320,b=201
從調(diào)試結(jié)果我們可以看出,當(dāng)我們點(diǎn)擊文本框后,根布局調(diào)用了onMeasure,onSizeChanged和onLayout。
實(shí)際上,當(dāng)設(shè)置為adjustResize后,軟鍵盤彈出時(shí),要對(duì)主窗口布局重新進(jìn)行measure和layout,而在layout時(shí),發(fā)現(xiàn)窗口的大小發(fā)生的變化,因此調(diào)用了onSizeChanged。
從下圖的運(yùn)行結(jié)果我們也可以看出,原本在下方的TextView被頂?shù)搅溯斎敕ǖ纳戏健?
模式二,平移模式
windowSoftInputMode的值如果設(shè)置為adjustPan,那么該Activity主窗口并不調(diào)整屏幕的大小以便留出軟鍵盤的空間。相反,當(dāng)前窗口的內(nèi)容將自動(dòng)移動(dòng)以便當(dāng)前焦點(diǎn)從不被鍵盤覆蓋和用戶能總是看到輸入內(nèi)容的部分。這個(gè)通常是不期望比調(diào)整大小,因?yàn)橛脩艨赡荜P(guān)閉軟鍵盤以便獲得與被覆蓋內(nèi)容的交互操作。
上面的例子中,我們將AndroidManifest.xml的屬性進(jìn)行更改:android: windowSoftInputMode = "adjustPan"
重新運(yùn)行,并點(diǎn)擊文本框,查看調(diào)試信息:
E/onMeasure 6(8378): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec=1073742200
E/onMeasure 7(8378): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec=1073742201
E/onLayout 8(8378): =>OnLayout called! l=0, t=0,r=320,b=377
我們看到:系統(tǒng)也重新進(jìn)行了measrue和layout,但是我們發(fā)現(xiàn),layout過程中onSizeChanged并沒有調(diào)用,這說明輸入法彈出前后并沒有改變?cè)胁季值拇笮 ?
從下圖的運(yùn)行結(jié)果我們可以看到,下方的TextView并沒有被頂?shù)捷斎敕ㄉ戏健?
事實(shí)上,當(dāng)輸入框不會(huì)被遮擋時(shí),該模式?jīng)]有對(duì)布局進(jìn)行調(diào)整,然而當(dāng)輸入框?qū)⒁徽趽鯐r(shí),窗口就會(huì)進(jìn)行平移。也就是說,該模式始終是保持輸入框?yàn)榭梢?。如下圖,整個(gè)窗口,包括標(biāo)題欄均被上移,以保證文本框可見。
模式三 自動(dòng)模式
當(dāng)屬性windowSoftInputMode被設(shè)置為adjustUspecified時(shí),它不被指定是否該Activity主窗口調(diào)整大小以便留出軟鍵盤的空間,或是否窗口上的內(nèi)容得到屏幕上當(dāng)前的焦點(diǎn)是可見的。系統(tǒng)將自動(dòng)選擇這些模式中一種主要依賴于是否窗口的內(nèi)容有任何布局視圖能夠滾動(dòng)他們的內(nèi)容。如果有這樣的一個(gè)視圖,這個(gè)窗口將調(diào)整大小,這樣的假設(shè)可以使?jié)L動(dòng)窗口的內(nèi)容在一個(gè)較小的區(qū)域中可見的。這個(gè)是主窗口默認(rèn)的行為設(shè)置。
也就是說,系統(tǒng)自動(dòng)決定是采用平移模式還是壓縮模式,決定因素在于內(nèi)容是否可以滾動(dòng)。
三、偵聽軟鍵盤的顯示隱藏
有時(shí)候,借助系統(tǒng)本身的機(jī)制來實(shí)現(xiàn)主窗口的調(diào)整并非我們想要的結(jié)果,我們可能希望在軟鍵盤顯示隱藏的時(shí)候,手動(dòng)的對(duì)布局進(jìn)行修改,以便使軟鍵盤彈出時(shí)更加美觀。這時(shí)就需要對(duì)軟鍵盤的顯示隱藏進(jìn)行偵聽。
直接對(duì)軟鍵盤的顯示隱藏偵聽的方法本人沒有找到,如果哪位找到的方法請(qǐng)務(wù)必告訴本人一聲。還有本方法針對(duì)壓縮模式,平移模式不一定有效。
我們可以借助軟鍵盤顯示和隱藏時(shí),對(duì)主窗口進(jìn)行了重新布局這個(gè)特性來進(jìn)行偵聽。如果我們?cè)O(shè)置的模式為壓縮模式,那么我們可以對(duì)布局的onSizeChanged函數(shù)進(jìn)行跟蹤,如果為平移模式,那么該函數(shù)可能不會(huì)被調(diào)用。
我們可以重寫根布局,因?yàn)楦季值母叨纫话闱闆r下是不發(fā)生變化的。
假設(shè)跟布局為線性布局,模式為壓縮模式,我們寫一個(gè)例子,當(dāng)輸入法彈出時(shí)隱藏某個(gè)view,輸入法隱藏時(shí)顯示某個(gè)view。
- public class ResizeLayout extends LinearLayout{
- private OnResizeListener mListener;
- public interface OnResizeListener {
- void OnResize( int w, int h, int oldw, int oldh);
- }
- public void setOnResizeListener(OnResizeListener l) {
- mListener = l;
- }
- public ResizeLayout(Context context, AttributeSet attrs) {
- super (context, attrs);
- }
- @Override
- protected void onSizeChanged( int w, int h, int oldw, int oldh) {
- super .onSizeChanged(w, h, oldw, oldh);
- if (mListener != null ) {
- mListener.OnResize(w, h, oldw, oldh);
- }
- }
- }
在我們的Activity中,通過如下方法調(diào)用:
- public class InputMethodTestActivity extends Activity {
- private static final int BIGGER = 1 ;
- private static final int SMALLER = 2 ;
- private static final int MSG_RESIZE = 1 ;
- private static final int HEIGHT_THREADHOLD = 30 ;
- class InputHandler extends Handler {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case MSG_RESIZE: {
- if (msg.arg1 == BIGGER) {
- findViewById(R.id.bottom_layout).setVisibility(View.VISIBLE);
- } else {
- findViewById(R.id.bottom_layout).setVisibility(View.GONE);
- }
- }
- break ;
- default :
- break ;
- }
- super .handleMessage(msg);
- }
- }
- private InputHandler mHandler = new InputHandler();
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super .onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ResizeLayout layout = (ResizeLayout) findViewById(R.id.root_layout);
- layout.setOnResizeListener( new ResizeLayout.OnResizeListener() {
- public void OnResize( int w, int h, int oldw, int oldh) {
- int change = BIGGER;
- if (h < oldh) {
- change = SMALLER;
- }
- Message msg = new Message();
- msg.what = 1 ;
- msg.arg1 = change;
- mHandler.sendMessage(msg);
- }
- });
- }
- }
這里特別需要注意的是,不能直接在OnResizeListener中對(duì)要改變的View進(jìn)行更改,因?yàn)镺nSizeChanged函數(shù)實(shí)際上是運(yùn)行在View的layout方法中,如果直接在onSizeChange中改變view的顯示屬性,那么很可能需要重新調(diào)用layout方法才能顯示正確。然而我們的方法又是在layout中調(diào)用的,因此會(huì)出現(xiàn)錯(cuò)誤。因此我們?cè)诶又胁捎昧薍andler的方法。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
