日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 软件盘弹回去的最好体验,Android 软键盘弹出 日常填坑

發布時間:2023/12/20 Android 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 软件盘弹回去的最好体验,Android 软键盘弹出 日常填坑 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

開發輸入框的開發者都會遇到一個問題,那就是在登錄界面時,當你點擊輸入框時,下邊的按鈕有時會被輸入框擋住,這個不利于用戶的體驗,所以很多人希望軟鍵盤彈出時,也能把按鈕擠上去。這樣的交互更人性化,做得合理。

我們可以在AndroidManifest.xml的Activity設置屬性:android:windowSoftInputMode = "adjustResize" ,軟鍵盤彈出時,要對主窗口布局重新進行布局,并調用onSizeChanged方法,切記一點當我們設置為“adjustResize”時,我們的界面不要設置為全屏模式,否則設置了這個屬性也不會有什么效果。而當我們設置android: windowSoftInputMode = "adjustPan"時,主窗口就不會調用onSizeChanged方法,界面的一部分就會被軟鍵盤覆蓋住,就不會被擠到軟鍵盤之上了。

我們通過一段代碼來測試一下,當我們設置了該屬性后,彈出輸入法時,系統做了什么:

1、重寫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、我們的布局設置為:

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、打印信息比對

AndroidManifest.xml的Activity設置屬性:android:windowSoftInputMode = "adjustResize"

運行程序,點擊文本框,查看調試信息:

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

從調試結果我們可以看出,當我們點擊文本框后,根布局調用了onMeasure,onSizeChanged和onLayout。

windowSoftInputMode的值如果設置為adjustPan,那么該Activity主窗口并不調整屏幕的大小以便留出軟鍵盤的空間。相反,當前窗口的內容將自動移動以便當前焦點從不被鍵盤覆蓋和用戶能總是看到輸入內容的部分。這個通常是不期望比調整大小,因為用戶可能關閉軟鍵盤以便獲得與被覆蓋內容的交互操作。

上面的例子中,我們將AndroidManifest.xml的屬性進行更改:android: windowSoftInputMode = "adjustPan"

重新運行,并點擊文本框,查看調試信息:

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

我們看到:系統也重新進行了measrue和layout,但是我們發現,layout過程中onSizeChanged并沒有調用,這說明輸入法彈出前后并沒有改變原有布局的大小。

當然還有其他屬性可以設置:

"stateUnspecified"

軟鍵盤的狀態(是否它是隱藏或可見)沒有被指定。系統將選擇一個合適的狀態或依賴于主題的設置。

這個是為了軟件盤行為默認的設置。

總結

以上是生活随笔為你收集整理的android 软件盘弹回去的最好体验,Android 软键盘弹出 日常填坑的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。