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

歡迎訪問 生活随笔!

生活随笔

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

Android

Android软键盘的显示与隐藏

發布時間:2024/4/14 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android软键盘的显示与隐藏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文僅是對原創文章重新進行或多或少的代碼測試,權當記錄并加深印象

再次感謝開發者社區的分享

?

一:簡述

點擊文本框EditText,系統會自動彈出軟鍵盤(其本質是一個Dialog),這必然會引起當前Activity主窗口的大小調整

而Android提供了不同的可選模式去調整活動窗口的大小,與之相關的屬性為:android:windowSoftInputMode, 當然具體的實現是由系統完成的

可以在清單文件Manifest.xml中的Activity標簽內設置

如:android:windowSoftInputMode="stateUnspecified|adjustPan"

該屬性可選的值有兩部分,一部分為軟鍵盤的狀態控制,另一部分是活動主窗口的調整。前一部分本文不做討論,請讀者自行查閱android文檔。

?

一: 壓縮模式

android:windowSoftInputMode="adjustResize", 那么不管活動主窗口壓縮后文本框EditText是否可見(這將于下面一種模式形成對比),

當前Activity主窗口頂部保持不變,總是被從下向上壓縮,壓縮的距離等于軟鍵盤的高度

測試代碼(Android2.3.3SDK):

public class CustomRelativeLayout extends RelativeLayout{??????? public CustomRelativeLayout(Context context, AttributeSet attrs) {??????? super(context, attrs);??? }??????????? @Override??? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {??????? Log.i("lanyan", "onMeasure");??????? super.onMeasure(widthMeasureSpec, heightMeasureSpec);??? }??????? @Override??? protected void onLayout(boolean changed, int l, int t, int r, int b) {??????? Log.i("lanyan", "onLayout");??????? super.onLayout(changed, l, t, r, b);??? }??????? /**???? * 當前活動主窗口大小改變時調用???? */??? @Override??? protected void onSizeChanged(int w, int h, int oldw, int oldh) {??????? Log.i("lanyan", "onSizeChanged");??????? super.onSizeChanged(w, h, oldw, oldh);??? }??? }

<com.lanyan.drawable.widget.CustomRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"??? android:layout_width="fill_parent"??? android:layout_height="fill_parent"??? android:orientation="vertical"??? >??????? <TextView android:layout_width="fill_parent"??? android:layout_height="20dp" android:background="#9999CC"??? android:layout_alignParentTop="true" android:text="============= 我在頂部 =============="??? android:textColor="#FFFFFF"/>??????????? <EditText android:layout_width="fill_parent"??? android:layout_height="wrap_content"???? android:layout_marginTop="220dp" />??????? <TextView android:layout_width="fill_parent"??? android:layout_height="20dp" android:background="#9999CC"??? android:layout_alignParentBottom="true" android:text="============= 我在底部 =============="??? android:textColor="#FFFFFF"/>com.lanyan.drawable.widget.CustomRelativeLayout>

運行程序,點擊文本框,日志順序如下:

onMeasure

onSizeChanged

onLayout

實際上,當設置為adjustResize后,軟鍵盤彈出時,要對主窗口布局重新進行measure和layout,而在layout時,發現窗口的大小發生的變化,因此調用了onSizeChanged

示意圖1:EditText距離底部的距離 < 軟鍵盤高度

可以看到, 頂部不變,底部被軟鍵盤頂了上去,文本框被遮住

2,微調xml布局,使得EditText距離底部的距離 > 軟鍵盤高度

?

二: 平移模式

android:windowSoftInputMode="adjustPan",此模式下,Activity主窗口大小始終保持不變,不管是否平移,

總是保證文本框不被軟鍵盤覆蓋且輸入內容可見

上述代碼的日志信息:

onMeasure

onLayout

可以看到,并不會調用onSizeChanged()方法

示意圖1, EditText距離底部距離 < 軟鍵盤高度

底部TextView并沒有被頂上去,而是活動主窗口整體向上平移(包括標題欄),具體平移的距離由系統measure,以便確保文本框可見

2,EditText距離底部的距離 > 軟鍵盤高度

與上面類似,只是窗口并未平移,因為即使軟鍵盤彈出,也不影響文本框是否可見,這種情況下,軟鍵盤等于是"浮動"在Activity上面

?

三: 自動模式

android:windowSoftInputMode="adjustUnspecified"

系統自動決定是采用平移模式還是壓縮模式,決定因素在于內容是否可以滾動。

?

二:監聽軟鍵盤的顯示隱藏

可以通過onSizeChanged()方法間接地對軟鍵盤的顯示隱藏進行監聽(并未精確到軟鍵盤顯示隱藏之前/之后這種程度),從而可以在主窗口大小發生變化時,進行自定義的操作,如顯示或隱藏某個view

?

由于View類并未提供類似setOnClickListener(....)這樣方便的接口,所以還是要重寫根布局,并且加個回調接口即可

?此方法僅當Activity為壓縮模式是有效,平移模式窗口大小不變,系統不會調用onSizeChanged()方法

public class CustomRelativeLayout extends RelativeLayout{??????? private KeyboardChangeListener listener;??????? public CustomRelativeLayout(Context context, AttributeSet attrs) {??????? super(context, attrs);??? }??????? @Override??? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {??????? Log.i("lanyan", "onMeasure");??????? super.onMeasure(widthMeasureSpec, heightMeasureSpec);??? }??????? @Override??? protected void onLayout(boolean changed, int l, int t, int r, int b) {??????? Log.i("lanyan", "onLayout");??????? super.onLayout(changed, l, t, r, b);??? }??????? /**???? * 當前活動主窗口大小改變時調用???? */??? @Override??? protected void onSizeChanged(int w, int h, int oldw, int oldh) {??????? Log.i("lanyan", "onSizeChanged");??????? super.onSizeChanged(w, h, oldw, oldh);??????????????? if (null != listener) {??????????? listener.onKeyboardChange(w, h, oldw, oldh);??????? }??? }??????? public void setOnKeyboardChangeListener(KeyboardChangeListener listener) {??????? this.listener = listener;??? }??????? /**???? * Activity主窗口大小改變時的回調接口(本示例中,等價于軟鍵盤顯示隱藏時的回調接口)???? * @author mo???? *???? */??? public interface KeyboardChangeListener {??????? public void onKeyboardChange(int w, int h, int oldw, int oldh);??? }??? }

public class EditActivity extends Activity{??? @Override??? protected void onCreate(Bundle savedInstanceState) {??????? super.onCreate(savedInstanceState);??????????????? setContentView(R.layout.edit);??????? CustomRelativeLayout customRelativeLayout = (CustomRelativeLayout)findViewById(R.id.relativelayout1);??????? customRelativeLayout.setOnKeyboardChangeListener(new CustomRelativeLayout.KeyboardChangeListener() {??????????????????????? @Override??????????? public void onKeyboardChange(int w, int h, int oldw, int oldh) {??????????????????????????????? //do your operation??????????????????????????? }??????? });??????????? }??? }

?

PS: 上述軟鍵盤的彈出都是點擊文本框,系統自動彈出的

也可以通過代碼的方式手動控制軟鍵盤的顯示與隱藏(Android2.3.3SDK)

tv.setOnClickListener(new OnClickListener() {??????????? @Override??????????? public void onClick(Vie

轉載于:https://blog.51cto.com/kaka123456/932529

總結

以上是生活随笔為你收集整理的Android软键盘的显示与隐藏的全部內容,希望文章能夠幫你解決所遇到的問題。

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