android軟鍵盤的顯示後隱藏

轉載:http://www.apkbus.com/android-59367-1-1.html


一:簡述

點擊文本框EditText,系統會自動彈出軟鍵盤(其本質是一個Dialog),這必然會引起當前Activity主窗口的大小調整
而Android提供了不同的可選模式去調整活動窗口的大小,與之相關的屬性爲:android:windowSoftInputMode, 當然具體的實現是由系統完成的
可以在清單文件Manifest.xml中的Activity標籤內設置
如:android:windowSoftInputMode="stateUnspecified|adjustPan"
該屬性可選的值有兩部分,一部分爲軟鍵盤的狀態控制,另一部分是活動主窗口的調整。前一部分本文不做討論,請讀者自行查閱android文檔。

一: 壓縮模式
android:windowSoftInputMode="adjustResize", 那麼不管活動主窗口壓縮後文本框EditText是否可見(這將於下面一種模式形成對比),
當前Activity主窗口頂部保持不變,總是被從下向上壓縮,壓縮的距離等於軟鍵盤的高度
測試代碼(Android2.3.3SDK):
  1. public class CustomRelativeLayout extends RelativeLayout{
  2.    
  3.     public CustomRelativeLayout(Context context, AttributeSet attrs) {
  4.         super(context, attrs);
  5.     }
  6.    
  7.    
  8.     @Override
  9.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  10.         Log.i("lanyan", "onMeasure");
  11.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  12.     }
  13.    
  14.     @Override
  15.     protected void onLayout(boolean changed, int l, int t, int r, int b) {
  16.         Log.i("lanyan", "onLayout");
  17.         super.onLayout(changed, l, t, r, b);
  18.     }
  19.    
  20.     /**
  21.      * 當前活動主窗口大小改變時調用
  22.      */
  23.     @Override
  24.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  25.         Log.i("lanyan", "onSizeChanged");
  26.         super.onSizeChanged(w, h, oldw, oldh);
  27.     }
  28.    
  29. }
複製代碼
  1. <com.lanyan.drawable.widget.customrelativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:layout_width="fill_parent"
  3.     android:layout_height="fill_parent"
  4.     android:orientation="vertical"
  5.     >
  6.    
  7.     <textview android:layout_width="fill_parent"
  8.     android:layout_height="20dp" android:background="#9999CC"
  9.     android:layout_alignParentTop="true" android:text="============= 我在頂部 =============="
  10.     android:textColor="#FFFFFF"/>   
  11.    
  12.     <edittext android:layout_width="fill_parent"
  13.     android:layout_height="wrap_content"
  14.     android:layout_marginTop="220dp" />
  15.    
  16.     <textview android:layout_width="fill_parent"
  17.     android:layout_height="20dp" android:background="#9999CC"
  18.     android:layout_alignParentBottom="true" android:text="============= 我在底部 =============="
  19.     android:textColor="#FFFFFF"/>

複製代碼
運行程序,點擊文本框,日誌順序如下:
onMeasure
onSizeChanged
onLayout
實際上,當設置爲adjustResize後,軟鍵盤彈出時,要對主窗口布局重新進行measure和layout,而在layout時,發現窗口的大小發生的變化,因此調用了onSizeChanged
示意圖1:EditText距離底部的距離 < 軟鍵盤高度
2012071500281924.png2012071500283045.png
可以看到, 頂部不變,底部被軟鍵盤頂了上去,文本框被遮住
2,微調xml佈局,使得EditText距離底部的距離 > 軟鍵盤高度
2012071500403320.png2012071500405871.png

二: 平移模式
android:windowSoftInputMode="adjustPan",此模式下,Activity主窗口大小始終保持不變,不管是否平移,
總是保證文本框不被軟鍵盤覆蓋且輸入內容可見
上述代碼的日誌信息:
onMeasure
onLayout
可以看到,並不會調用onSizeChanged()方法
示意圖1, EditText距離底部距離 < 軟鍵盤高度
2012071500443369.png2012071500452269.png
底部TextView並沒有被頂上去,而是活動主窗口整體向上平移(包括標題欄),具體平移的距離由系統measure,以便確保文本框可見
2,EditText距離底部的距離 > 軟鍵盤高度

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

三: 自動模式
android:windowSoftInputMode="adjustUnspecified"
系統自動決定是採用平移模式還是壓縮模式,決定因素在於內容是否可以滾動。

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

由於View類並未提供類似setOnClickListener(....)這樣方便的接口,所以還是要重寫根佈局,並且加個回調接口即可
此方法僅當Activity爲壓縮模式是有效,平移模式窗口大小不變,系統不會調用onSizeChanged()方法
  1. public class CustomRelativeLayout extends RelativeLayout{
  2.    
  3.     private KeyboardChangeListener listener;
  4.    
  5.     public CustomRelativeLayout(Context context, AttributeSet attrs) {
  6.         super(context, attrs);
  7.     }
  8.    
  9.     @Override
  10.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  11.         Log.i("lanyan", "onMeasure");
  12.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  13.     }
  14.    
  15.     @Override
  16.     protected void onLayout(boolean changed, int l, int t, int r, int b) {
  17.         Log.i("lanyan", "onLayout");
  18.         super.onLayout(changed, l, t, r, b);
  19.     }
  20.    
  21.     /**
  22.      * 當前活動主窗口大小改變時調用
  23.      */
  24.     @Override
  25.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  26.         Log.i("lanyan", "onSizeChanged");
  27.         super.onSizeChanged(w, h, oldw, oldh);
  28.         
  29.         if (null != listener) {
  30.             listener.onKeyboardChange(w, h, oldw, oldh);
  31.         }
  32.     }
  33.    
  34.     public void setOnKeyboardChangeListener(KeyboardChangeListener listener) {
  35.         this.listener = listener;
  36.     }
  37.    
  38.     /**
  39.      * Activity主窗口大小改變時的回調接口(本示例中,等價於軟鍵盤顯示隱藏時的回調接口)
  40.      * @author mo
  41.      *
  42.      */
  43.     public interface KeyboardChangeListener {
  44.         public void onKeyboardChange(int w, int h, int oldw, int oldh);
  45.     }
  46.    
  47. }
複製代碼
  1. public class EditActivity extends Activity{

  2.     @Override
  3.     protected void onCreate(Bundle savedInstanceState) {
  4.         super.onCreate(savedInstanceState);
  5.         
  6.         setContentView(R.layout.edit);
  7.         CustomRelativeLayout customRelativeLayout = (CustomRelativeLayout)findViewById(R.id.relativelayout1);
  8.         customRelativeLayout.setOnKeyboardChangeListener(new CustomRelativeLayout.KeyboardChangeListener() {
  9.             
  10.             @Override
  11.             public void onKeyboardChange(int w, int h, int oldw, int oldh) {
  12.                
  13.                 //do your operation
  14.                
  15.             }
  16.         });
  17.         
  18.     }
  19.    
  20. }
複製代碼
PS: 上述軟鍵盤的彈出都是點擊文本框,系統自動彈出的
也可以通過代碼的方式手動控制軟鍵盤的顯示與隱藏(Android2.3.3SDK)
  1. tv.setOnClickListener(new OnClickListener() {
  2.             @Override
  3.             public void onClick(View v) {
  4.                 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  5.                 //隱藏軟鍵盤
  6. //                imm.hideSoftInputFromWindow(tv.getWindowToken(), 0);
  7.                 //顯示軟鍵盤
  8. //                imm.showSoftInputFromInputMethod(tv.getWindowToken(), 0);
  9.                 //切換軟鍵盤的顯示與隱藏
  10.                 imm.toggleSoftInputFromWindow(tv.getWindowToken(), 0, InputMethodManager.HIDE_NOT_ALWAYS);
  11.                 //或者
  12. //                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
  13.             }
  14.         });
複製代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章