监听软键盘的隐藏和弹出

public class MyRelativeLayout extends RelativeLayout{

	public static final byte KEYBOARD_STATE_SHOW = -3;
	public static final byte KEYBOARD_STATE_HIDE = -2;
	public static final byte KEYBOARD_STATE_INIT = -1;
	
	private boolean mHasInit;
	private boolean mHasKeybord;
	private int mHeight;
	private onKybdsChangeListener mListener;
	
	public MyRelativeLayout(Context context) {
		super(context);
	}

	public MyRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public MyRelativeLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public void setOnkbdStateListener(onKybdsChangeListener listener){
		mListener = listener;
	}
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		Log.i("------------", b+"");
		if (!mHasInit) {//初始化
			
				mHasInit = true;
				mHeight = b;
				if (mListener != null) {
				    mListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT);
				}
				
		} else {
		
				mHeight = mHeight < b ? b : mHeight;
		}
		
		if (mHasInit && mHeight > b) {
				mHasKeybord = true;
				if (mListener != null) {
					mListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW);
				}
				Log.w("tag", "show keyboard.......");
		}
			
		if (mHasInit && mHasKeybord && mHeight == b) {
				mHasKeybord = false;
				if (mListener != null) {
					mListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE);
				}
				Log.w("tag", "hide keyboard.......");
		}
	 }

	public interface onKybdsChangeListener{
		public void onKeyBoardStateChange(int state);
	}
}
<com.example.softinput.MyRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboardLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:fillViewport="true" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/testone_tv"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#000000"
                android:gravity="center"
                android:text="软件盘弹起,我将消失!软键盘隐藏,我将回来!"
                android:layout_weight="1.0"
                android:textColor="#00ff00"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/testone_et"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"></EditText>
        </LinearLayout>
    </ScrollView>

</com.example.softinput.MyRelativeLayout>

package com.example.softinput;

import com.example.softinput.MyRelativeLayout.onKybdsChangeListener;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);
		
		 MyRelativeLayout mainView = (MyRelativeLayout) findViewById(R.id.keyboardLayout1);
         final TextView tv = (TextView) findViewById(R.id.testone_tv);
         mainView.setOnkbdStateListener(new onKybdsChangeListener() {
                 
                 public void onKeyBoardStateChange(int state) {
                     switch (state) {
                     case MyRelativeLayout.KEYBOARD_STATE_HIDE:
                             tv.setVisibility(View.VISIBLE);
                             Toast.makeText(getApplicationContext(),
                            		 "软键盘隐藏", Toast.LENGTH_SHORT).show();
                             break;
                     case MyRelativeLayout.KEYBOARD_STATE_SHOW:
                             tv.setVisibility(View.INVISIBLE);
                             Toast.makeText(getApplicationContext(),
                            		 "软键盘弹起", Toast.LENGTH_SHORT).show();
                             break;
                     }
                 }
         });
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值