Android軟鍵盤的隱藏顯示對輸入框和布局的影響。
1. 平移模式:android:windowSoftInputMode="adjustPan" layout 檔案:
<com.hualu.softinput.RelativeLayoutResize xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="25dp" android:text="@string/hello_world" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:ems="10" > </EditText></com.hualu.softinput.RelativeLayoutResize>
com.hualu.softinput.RelativeLayoutResize:
package com.hualu.softinput;import android.content.Context;import android.util.AttributeSet;import android.widget.RelativeLayout;public class RelativeLayoutResize extends RelativeLayout {private OnRelativeLayoutResizeListener listener ;public RelativeLayoutResize(Context context) {super(context);}public RelativeLayoutResize(Context context, AttributeSet attrs) {super(context, attrs) ;}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);System.out.println("onMeasure() ---> width = " + widthMeasureSpec + " , height = " + heightMeasureSpec);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {super.onLayout(changed, l, t, r, b);System.out.println("onLayout() -----> changed = " + changed + " , l = " + l + " , t = " + t + " , r = " + r + " , b = " + b );}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);System.out.println("onSizeChanged() ----> w = " + w + " , h = " + h + " , oldW = " + oldw + " , oldH = " + oldh);if(listener != null && h < oldh){listener.onResize() ;}}public void setOnRelativeLayoutResizeListener(OnRelativeLayoutResizeListener listener){this.listener = listener ;}public interface OnRelativeLayoutResizeListener{void onResize() ;}}
啟動應用的介面:
列印的Log:
IME起來:
Log資訊:
當介面改變布局時:
<com.hualu.softinput.RelativeLayoutResize xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="20dp" android:ems="10" > <requestFocus /> </EditText> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginBottom="64dp" android:text="@string/hello_world" /> </com.hualu.softinput.RelativeLayoutResize>
啟動介面為:
Log資訊:
啟動IME之後:
Log資訊:
結論:
從上面log資訊,可以得出在平移模式下,啟動IME和退出IME都不會出發onSizeChange()方法。
2. 壓縮調整模式android:windowSoftInputMode="adjustResize"
當為藍色Layout時:
啟動介面:
Log資訊:
退出IME之後:
Log資訊:
當Layout為綠色的內容時:
啟動應用介面:
Log資訊:
退出IME之後:
Log資訊:
結論:
在壓縮模式下, IME的啟動和退出都會觸發onSizeChange()方法的調用。
3. 壓縮模式,監聽IME的顯示和隱藏:
因此,在壓縮模式下可以做到監聽IME的顯示與隱藏。
方法如com.hualu.softinput.RelativeLayoutResize中在onSizeChange()方法裡面自訂一個事件,監聽高度的變法:
if(listener != null && h < oldh){listener.onResize() ;}
在Activity中的調用是:
package com.hualu.softinput;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.TextView;import com.hualu.softinput.RelativeLayoutResize.OnRelativeLayoutResizeListener;public class MainActivity extends Activity {private TextView text ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);text = (TextView)this.findViewById(R.id.text) ;RelativeLayoutResize rlr = (RelativeLayoutResize)this.findViewById(R.id.contenter) ;rlr.setFocusableInTouchMode(true ) ;rlr.setOnRelativeLayoutResizeListener(new OnRelativeLayoutResizeListener() {@Overridepublic void onResize() {text.setVisibility(View.GONE) ;}}) ;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}