android 软件盘弹回去的最好体验,Android 软键盘弹出 日常填坑
開(kāi)發(fā)輸入框的開(kāi)發(fā)者都會(huì)遇到一個(gè)問(wèn)題,那就是在登錄界面時(shí),當(dāng)你點(diǎn)擊輸入框時(shí),下邊的按鈕有時(shí)會(huì)被輸入框擋住,這個(gè)不利于用戶(hù)的體驗(yàn),所以很多人希望軟鍵盤(pán)彈出時(shí),也能把按鈕擠上去。這樣的交互更人性化,做得合理。
我們可以在AndroidManifest.xml的Activity設(shè)置屬性:android:windowSoftInputMode = "adjustResize" ,軟鍵盤(pán)彈出時(shí),要對(duì)主窗口布局重新進(jìn)行布局,并調(diào)用onSizeChanged方法,切記一點(diǎn)當(dāng)我們?cè)O(shè)置為“adjustResize”時(shí),我們的界面不要設(shè)置為全屏模式,否則設(shè)置了這個(gè)屬性也不會(huì)有什么效果。而當(dāng)我們?cè)O(shè)置android: windowSoftInputMode = "adjustPan"時(shí),主窗口就不會(huì)調(diào)用onSizeChanged方法,界面的一部分就會(huì)被軟鍵盤(pán)覆蓋住,就不會(huì)被擠到軟鍵盤(pán)之上了。
我們通過(guò)一段代碼來(lái)測(cè)試一下,當(dāng)我們?cè)O(shè)置了該屬性后,彈出輸入法時(shí),系統(tǒng)做了什么:
1、重寫(xiě)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);
}
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);
}
2、我們的布局設(shè)置為:
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"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android:id="@+id/bottom_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="bottom">s
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:background="#77777777"
/>
3、打印信息比對(duì)
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。
windowSoftInputMode的值如果設(shè)置為adjustPan,那么該Activity主窗口并不調(diào)整屏幕的大小以便留出軟鍵盤(pán)的空間。相反,當(dāng)前窗口的內(nèi)容將自動(dòng)移動(dòng)以便當(dāng)前焦點(diǎn)從不被鍵盤(pán)覆蓋和用戶(hù)能總是看到輸入內(nèi)容的部分。這個(gè)通常是不期望比調(diào)整大小,因?yàn)橛脩?hù)可能關(guān)閉軟鍵盤(pá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過(guò)程中onSizeChanged并沒(méi)有調(diào)用,這說(shuō)明輸入法彈出前后并沒(méi)有改變?cè)胁季值拇笮 ?/p>
當(dāng)然還有其他屬性可以設(shè)置:
"stateUnspecified"
軟鍵盤(pán)的狀態(tài)(是否它是隱藏或可見(jiàn))沒(méi)有被指定。系統(tǒng)將選擇一個(gè)合適的狀態(tài)或依賴(lài)于主題的設(shè)置。
這個(gè)是為了軟件盤(pán)行為默認(rèn)的設(shè)置。
總結(jié)
以上是生活随笔為你收集整理的android 软件盘弹回去的最好体验,Android 软键盘弹出 日常填坑的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: uboot的一般性介绍
- 下一篇: android http最新框架,And