android软键盘现实和隐藏的监听

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

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android应用程序中监听软键盘状态并在软键盘打开时将其隐藏,可以使用以下代码: 1. 首先,在你的 activity 的 onCreate 方法中获取根布局并设置以下属性: ``` final View activityRootView = findViewById(R.id.activityRoot); activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight(); if (heightDiff > dpToPx(MyActivity.this, 200)) { // 如果高度差超过 200 dp,就认为软键盘打开了 hideSoftKeyboard(MyActivity.this); // 隐藏软键盘 } } }); ``` 其中,`activityRootView` 是你的 activity 的根布局,`OnGlobalLayoutListener` 是一个监听布局变化的接口,`heightDiff` 是根布局高度和 activity 高度的差值,`dpToPx` 是一个将 dp 转化为 px 的方法,`hideSoftKeyboard` 是将软键盘隐藏的方法。 2. 然后,编写 `dpToPx` 和 `hideSoftKeyboard` 方法: ``` public static int dpToPx(Context context, int dp) { DisplayMetrics metrics = context.getResources().getDisplayMetrics(); return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics); } public static void hideSoftKeyboard(Activity activity) { InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); View currentFocusView = activity.getCurrentFocus(); if (currentFocusView != null) { inputMethodManager.hideSoftInputFromWindow(currentFocusView.getWindowToken(), 0); } } ``` 其中,`dpToPx` 方法将 dp 转化为 px,`hideSoftKeyboard` 方法接收一个 Activity 对象并使用 `InputMethodManager` 和 `getCurrentFocus` 方法获取当前的焦点视图并将软键盘隐藏

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值