Android-监听软键盘状态

时间:2022-02-15 23:40:38

检测软键盘是否显示或隐藏的功能,下面展示下效果图:

Android-监听软键盘状态

我们接下来看实现:

activity_main_xml:

<com.zihao.view.InputMethodLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/input_method_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/title_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="这是一个软键盘监听Demo" />

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/title_tv" >

<EditText
android:id="@+id/mEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="监听软键盘是否弹出" />
</ScrollView>

</com.zihao.view.InputMethodLayout>

InputMethodLayout.java:

package com.zihao.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;

/**
* 一个继承自RelativeLayout的输入法监听布局
*
*/
public class InputMethodLayout extends RelativeLayout {

private static final String TAG = "InputMethodLayout";
/** 初始化状态 **/
public static final byte KEYBOARD_STATE_INIT = -1;
/** 隐藏状态 **/
public static final byte KEYBOARD_STATE_HIDE = -2;
/** 打开状态 **/
public static final byte KEYBOARD_STATE_SHOW = -3;
private boolean isInit;// 是否为初始化状态
private boolean hasKeybord;// 标识是否打开了软键盘
private int viewHeight;// 布局高度
private onKeyboardsChangeListener keyboarddsChangeListener;// 键盘状态监听

public InputMethodLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}

public InputMethodLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

public InputMethodLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

/**
* 设置软键盘状态监听
*
* @param listener
*/
public void setOnkeyboarddStateListener(onKeyboardsChangeListener listener) {
keyboarddsChangeListener = listener;
}

/**
* 布局状态发生改变时,会触发onLayout
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (!isInit) {
isInit = true;
viewHeight = b;
keyboardSateChange(KEYBOARD_STATE_INIT);
} else {
viewHeight = viewHeight < b ? b : viewHeight;
}
if (isInit && viewHeight > b) {
hasKeybord = true;
keyboardSateChange(KEYBOARD_STATE_SHOW);
Log.w(TAG, "显示软键盘");
}
if (isInit && hasKeybord && viewHeight == b) {
hasKeybord = false;
keyboardSateChange(KEYBOARD_STATE_HIDE);
Log.w(TAG, "隐藏软键盘");
}
}

/**
* 切换软键盘状态
*
* @param state
* // 状态
*/
public void keyboardSateChange(int state) {
if (keyboarddsChangeListener != null) {
keyboarddsChangeListener.onKeyBoardStateChange(state);
}
}

/**
* 软键盘状态切换监听
*
* @author zihao
*
*/
public interface onKeyboardsChangeListener {
public void onKeyBoardStateChange(int state);
}
}

MainActivity.java

package com.zihao;

import com.zihao.view.InputMethodLayout;
import com.zihao.view.InputMethodLayout.onKeyboardsChangeListener;

import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {

private InputMethodLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}

/**
* 初始化视图
*/
private void initView() {
layout = (InputMethodLayout) findViewById(R.id.input_method_layout);
layout.setOnkeyboarddStateListener(new onKeyboardsChangeListener() {// 监听软键盘状态

@Override
public void onKeyBoardStateChange(int state) {
// TODO Auto-generated method stub
switch (state) {
case InputMethodLayout.KEYBOARD_STATE_SHOW:
Toast.makeText(MainActivity.this, "打开软键盘", 1000).show();
break;
case InputMethodLayout.KEYBOARD_STATE_HIDE:
Toast.makeText(MainActivity.this, "关闭软键盘", 1000).show();
break;
}
}
});
}
}

图片显示的功能就实现啦!

下载地址如下:

http://download.csdn.net/detail/hehaiminginadth/9104141