一: 壓縮模式
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(Vi
Android軟鍵盤的顯示與隱藏
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.