原創文章傳送門:http://winuxxan.blog.51cto.com/2779763/522810
本文僅是對原創文章重新進行或多或少的代碼測試,權當記錄並加深印象
再次感謝原創作者的分享
一:簡述
點擊文本框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(View v) { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); //隱藏軟鍵盤 // imm.hideSoftInputFromWindow(tv.getWindowToken(), 0); //顯示軟鍵盤 // imm.showSoftInputFromInputMethod(tv.getWindowToken(), 0); //切換軟鍵盤的顯示與隱藏 imm.toggleSoftInputFromWindow(tv.getWindowToken(), 0, InputMethodManager.HIDE_NOT_ALWAYS); //或者 // imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); } });