importandroid.app.Activity;
importandroid.graphics.Rect;
importandroid.view.View;
importandroid.view.ViewTreeObserver;
importandroid.widget.Toast;/**
* Created by liujinhua on 15/10/25.
*/
public class SoftKeyBoardListener {
private View rootView; //activity的根视图
int rootViewVisibleHeight; //纪录根视图的显示高度
private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;
public SoftKeyBoardListener(Activity activity) {
//获取activity的根视图
rootView = activity.getWindow().getDecorView();
//监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//获取当前根视图在屏幕上显示的大小
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
int visibleHeight = r.height();
System.out.println(""+visibleHeight);
if (rootViewVisibleHeight == 0) {
rootViewVisibleHeight = visibleHeight;
return;
}
//根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
if (rootViewVisibleHeight == visibleHeight) {
return;
}
//根视图显示高度变小超过200,可以看作软键盘显示了
if (rootViewVisibleHeight - visibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}
//根视图显示高度变大超过200,可以看作软键盘隐藏了
if (visibleHeight - rootViewVisibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}
}
});
}
private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
}
public interface OnSoftKeyBoardChangeListener {
void keyBoardShow(int height);
void keyBoardHide(int height);
}
public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
}
}
转载链接:https://juejin.im/post/5a31e42ff265da43163d14fe
声明:侵删
参考:https://blog.csdn.net/liaoyi_/article/details/64441103
我用这个方法主要是为了解决fragment返回事件的监听,
fragment返回事件的监听方式一
在Activity里重写返回事件>遍历返回fragment>找到自己的fragment>调用自己fragment里自己定义的返回方法
这个方法有尝试但是没能解决我的问题,理论上可行
fragment返回事件的监听方式二
basefragmen里在init方法里整个布局获取焦点>在init方法里添加键盘监听>在键盘监听收起的方法里添加整个布局获取焦点>basefragmen抽象一个返回方法>子fragment强制重写返回方法
我用的方式二
package com.hebky.ios.base;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.hebky.ios.utils.SoftKeyBoardListener;
import com.trello.rxlifecycle2.components.RxFragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public abstract class BaseFrag<V, P extends BasePresenter> extends RxFragment {
public Activity mActivity;
protected View mRootView;
protected P mPresenter;
// private Unbinder unbinder;
protected abstract int getLayoutId();
protected abstract P createPresenter();
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mRootView = inflater.inflate(getLayoutId(), container, false);
mPresenter = createPresenter();
mPresenter.attachView((V) this);
return mRootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init(view);
initView();
initData();
initEvent();
}
private void init(View view) {
// unbinder = ButterKnife.bind(this, view);
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(backlistener);
mActivity = getActivity();
SoftKeyBoardListener.setListener(mActivity,new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
@Override
public void keyBoardShow(int height) {
//Toast.makeText(getActivity(), "键盘显示 高度" + height, Toast.LENGTH_SHORT).show();
// introl_iv.setBackground(null); //使LOGO消失
// tip("0001");
}
@Override
public void keyBoardHide(int height) {
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(backlistener);
// tip("0002");
//Toast.makeText(getActivity(), "键盘隐藏 高度" + height, Toast.LENGTH_SHORT).show();
// introl_iv.setBackgroundResource(R.drawable.intro_logo);//设置
}
});
}
private View.OnKeyListener backlistener = (v, keyCode, event) -> {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
//这边判断,如果是back的按键被点击了 就自己拦截实现掉
if (keyCode == KeyEvent.KEYCODE_BACK) {
return onBackPressed();//表示处理了
}
}
return false;
};
/**
* 初始化View
*/
protected void initView() {
}
/**
* 初始化数据
*/
protected void initData() {
}
/**
* 初始化事件
*/
protected void initEvent() {
// getFocus();
}
@Override
public void onDestroy() {
if (mPresenter != null) {
mPresenter.detachView();
}
// if (unbinder != null) {
// unbinder.unbind();
// }
super.onDestroy();
//监控Fragment对象
// RefWatcher refWatcher = LeakApplication.getRefWatcher();
// refWatcher.watch(this);
}
//主界面获取焦点
private void getFocus() {
if (getView() != null) {
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener((v, keyCode, event) -> {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
return onBackPressed();
}
return false;
});
}
}
/** fragment返回键的监听 */
protected abstract boolean onBackPressed();
protected void tip(String msg) {
if (getActivity() != null) {
((BaseAct) getActivity()).tip(msg);
}
}
}
感谢键盘监听类的作者,后面的是我自己原创,大家可以随意转载,前面的转载请保留原作者出处