Android軟鍵盤的隱藏顯示

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) ;
	}
	

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		System.out.println("onMeasure() ---> width = " +  widthMeasureSpec + " , height = " + heightMeasureSpec);
	}
	
	@Override
	protected 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 );
	}
	
	@Override
	protected 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:


輸入法起來:


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信息:


啓動輸入法之後:



Log信息:




結論:

從上面log信息,可以得出在平移模式下,啓動輸入法和退出輸入法都不會出發onSizeChange()方法。



2. 壓縮調整模式android:windowSoftInputMode="adjustResize"

當爲藍色Layout時:

啓動界面:


Log信息:


退出輸入法之後:


Log信息:



當Layout爲綠色的內容時:

啓動應用界面:


Log信息:


退出輸入法之後:


Log信息:



結論:

在壓縮模式下, 輸入法的啓動和退出都會觸發onSizeChange()方法的調用。


3. 壓縮模式,監聽輸入法的顯示和隱藏:

因此,在壓縮模式下可以做到監聽輸入法的顯示與隱藏。

方法如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 ;
	
	@Override
	protected 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() {
			
			@Override
			public void onResize() {
				text.setVisibility(View.GONE) ;
			}
		}) ;
	}

	@Override
	public 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;
	}

}
















發佈了241 篇原創文章 · 獲贊 3 · 訪問量 54萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章