栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android软键盘弹出和收起的监听

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android软键盘弹出和收起的监听

Android软键盘弹出和收起的监听

1.直接调用

SoftKeyboardHelper softKeyboardHelper = softKeyboardHelper = new SoftKeyboardHelper(this.context);
softKeyboardHelper.setKeyboardListener(rootView, new SoftKeyboardHelper.SoftKeyboardListener() {
            @Override
            public void onSoftKeyboardShow(int softKeyboardHeight) {
                ToastUtil.showToast("键盘弹出");
                }
            }

            @Override
            public void onSoftKeyboardHide(int softKeyboardHeight) {
                ToastUtil.showToast("键盘收起");
                }
            }
        });
  1. SoftKeyboardHelper.class
public class SoftKeyboardHelper {
    public static final String TAG = "SoftKeyboard_Debug";
    private InputMethodManager imm;
    private final Context context;
    private int lastScreenOrientation;

    
    public SoftKeyboardHelper(Context context) {
        this.context = context;
        if (ScreenUtils.isPortrait()) {
            lastScreenOrientation = Configuration.ORIENTATION_PORTRAIT;
        } else {
            lastScreenOrientation = Configuration.ORIENTATION_LANDSCAPE;
        }
    }


    
    public synchronized void showSoftKeyboard(EditText view) {
        if (imm == null) {
            imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        }
        imm.showSoftInput(view, 0);
    }

    
    public synchronized void hideSoftKeyboard(EditText view) {
        if (imm == null) {
            imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        }
        if (view != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }


    
    public void setKeyboardListener(final View parentLayout, final SoftKeyboardListener softKeyboardListener) {
        if (softKeyboardListener == null || parentLayout == null) {
            return;
        }
        parentLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View view, int left, int top, int right, int bottom,
                                       int oldLeft, int oldTop, int oldRight, int oldBottom) {
                int screenHeight = ScreenUtils.getScreenHeight();
                int currentScreenOrientation = -1;
                if (ScreenUtils.isPortrait()) {
                    currentScreenOrientation = Configuration.ORIENTATION_PORTRAIT;
                } else {
                    currentScreenOrientation = Configuration.ORIENTATION_LANDSCAPE;
                }
                //排除横竖屏切换引起的布局变化
                if (lastScreenOrientation != currentScreenOrientation) {
                    lastScreenOrientation = currentScreenOrientation;
                    return;
                }

                int defaultHeight = screenHeight / 3;
                if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > defaultHeight)) {
                    softKeyboardListener.onSoftKeyboardShow(0);
                } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > defaultHeight)) {
                    softKeyboardListener.onSoftKeyboardHide(0);
                }

            }
        });
    }


    
    public interface SoftKeyboardListener {
        
        void onSoftKeyboardShow(int softKeyboardHeight);

        
        void onSoftKeyboardHide(int softKeyboardHeight);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/826073.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号