package com.example.qq.support;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
public class KeyboardHelper {
private Activity activity;
private OnKeyboardStatusChangeListener onKeyboardStatusChangeListener;
private int windowBottom = -1;
private int keyboardHeight = 0;
public KeyboardHelper(Activity activity) {
this.activity = activity;
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
if (activity.getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
public void onCreate() {
View content = activity.findViewById(android.R.id.content);
// content.addOnLayoutChangeListener(listener); 这个方法有时会出现一些问题
content.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
}
public void onDestroy() {
View content = activity.findViewById(android.R.id.content);
ViewUtils.removeOnGlobalLayoutListener(content, onGlobalLayoutListener);
}
private OnGlobalLayoutListener onGlobalLayoutListener = new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
Log.d("KeyboardHelper", "onGlobalLayout: " + rect + ", " + windowBottom);
int newBottom = rect.bottom;
if (windowBottom != -1 && windowBottom != newBottom) {
if (newBottom < windowBottom) {
// keyboard pop
keyboardHeight = windowBottom - newBottom;
if (onKeyboardStatusChangeListener != null) {
onKeyboardStatusChangeListener.OnKeyBoardPop(keyboardHeight);
}
} else {
// keyboard close
if (onKeyboardStatusChangeListener != null) {
onKeyboardStatusChangeListener.OnKeyBoardClose(keyboardHeight);
}
}
}
windowBottom = newBottom;
}
};
public void setOnKeyboardStatusChangeListener(
OnKeyboardStatusChangeListener onKeyboardStatusChangeListener) {
this.onKeyboardStatusChangeListener = onKeyboardStatusChangeListener;
}
public void showKeyboard(final View editText) {
editText.requestFocus();
InputMethodManager manager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInput(editText, 0);
}
public void hideKeyboard(View editText) {
InputMethodManager manager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
manager.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
public interface OnKeyboardStatusChangeListener {
void OnKeyBoardPop(int keyboardHeight);
void OnKeyBoardClose(int keyboardHeight);
}
}
模仿QQ登录界面解决软键盘遮挡按钮问题
需积分: 16 127 浏览量
2018-07-14
15:49:28
上传
评论
收藏 78KB ZIP 举报
leandom
- 粉丝: 3
- 资源: 7
最新资源
- SDN相关软件包,ryn控制器,python环境等
- 基于PWM方法控制音调,支持高、低两个阶段的音调
- 524springboot + vue 在线动漫信息平台.zip(可运行源码+数据库文件+文档)
- 基于QT+QML+C++实现的显示fps帧率的控件+源码
- Java毕业设计-基于Springboot+Vue水果购物网站源码+数据库+PPT文档+演示视频(高分项目).zip
- AT Disk Imager 一个可以自动烧录树莓派镜像的软件
- 523springboot + vue 点餐平台网站.zip(可运行源码+数据库文件+文档)
- 4-2-2.zip
- 522springboot + vue 租房网站.zip(可运行源码+数据库文件+文档)
- Java毕业设计-基于Springboot+Vue大学生竞赛管理系统-源码+数据库+演示视频(高分项目).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈