android软键盘现实和隐藏的监听
2015-07-03 14:37
603 查看
http://stackoverflow.com/questions/4312319/howto-capture-the-virtual-keyboard-show-hide-event-in-android
http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android?lq=1
大概的方法就是监听view的的高度的变化,来判断软键盘的显示和隐藏。
注意:一点就是属性要设置的好,很多情况下试adjustResize才有效。
import java.util.Arraylist;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
public class KeyboardDetectorRelativeLayout extends RelativeLayout {
public interface IKeyboardChanged {
void onKeyboardShown();
void onKeyboardHidden();
}
private ArrayList<IKeyboardChanged> keyboardListener = new ArrayList<IKeyboardChanged>();
public KeyboardDetectorRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public KeyboardDetectorRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public KeyboardDetectorRelativeLayout(Context context) {
super(context);
}
public void addKeyboardStateChangedListener(IKeyboardChanged listener) {
keyboardListener.add(listener);
}
public void removeKeyboardStateChangedListener(IKeyboardChanged listener) {
keyboardListener.remove(listener);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
final int actualHeight = getHeight();
if (actualHeight > proposedheight) {
notifyKeyboardShown();
} else if (actualHeight < proposedheight) {
notifyKeyboardHidden();
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private void notifyKeyboardHidden() {
for (IKeyboardChanged listener : keyboardListener) {
listener.onKeyboardHidden();
}
}
private void notifyKeyboardShown() {
for (IKeyboardChanged listener : keyboardListener) {
listener.onKeyboardShown();
}
}
}
http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android?lq=1
大概的方法就是监听view的的高度的变化,来判断软键盘的显示和隐藏。
注意:一点就是属性要设置的好,很多情况下试adjustResize才有效。
import java.util.Arraylist;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
public class KeyboardDetectorRelativeLayout extends RelativeLayout {
public interface IKeyboardChanged {
void onKeyboardShown();
void onKeyboardHidden();
}
private ArrayList<IKeyboardChanged> keyboardListener = new ArrayList<IKeyboardChanged>();
public KeyboardDetectorRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public KeyboardDetectorRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public KeyboardDetectorRelativeLayout(Context context) {
super(context);
}
public void addKeyboardStateChangedListener(IKeyboardChanged listener) {
keyboardListener.add(listener);
}
public void removeKeyboardStateChangedListener(IKeyboardChanged listener) {
keyboardListener.remove(listener);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
final int actualHeight = getHeight();
if (actualHeight > proposedheight) {
notifyKeyboardShown();
} else if (actualHeight < proposedheight) {
notifyKeyboardHidden();
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private void notifyKeyboardHidden() {
for (IKeyboardChanged listener : keyboardListener) {
listener.onKeyboardHidden();
}
}
private void notifyKeyboardShown() {
for (IKeyboardChanged listener : keyboardListener) {
listener.onKeyboardShown();
}
}
}
相关文章推荐
- Android QQ登录验证的小例子
- android 5.2
- Android入门(5)——多状态按钮ToggleButton、多选按钮CheckBox、RadioGroup与RadioButtont按钮
- Android的消息机制
- Android Studio使用OpenCV的配置方法
- android工程里,配置文件的实现。
- Android RGB颜色对照表
- Android WebService
- android之AlertDialog 点击其他区域自动消失
- android之AlertDialog 点击其他区域自动消失
- android之AlertDialog 点击其他区域自动消失
- android之AlertDialog 点击其他区域自动消失
- Android Home and back
- 我的Android Studio 优化之路
- Android下常见的内存泄露
- Android 大约Dialog弹出窗口
- Android原型界面设计工具
- Android Studio使用OpenCV的配置方法
- android gradle task dependence
- android调用系统相机拍照并裁剪