Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱
MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina.com

目录

软键盘相关问题

我们在开发中经常会遇到软键盘遮挡住了输入框,而直接把输入框往上顶adjustResize这种方法过于暴力影响美观,我们希望键盘弹出时动态的去改变布局。这时候,便用到了监听键盘弹起事件的功能。

可能大家都会潜意识觉得:Android中没有可以复写的方法么?或者说,没有什么listener可以让我们使用么?

抱歉,真的没有,我们潜意识都是以为系统会提供,其实系统提供的是InputMethodManager,让我们控制键盘的弹出和隐藏,而不是键盘弹出和隐藏触发事件。

Android系统并没有给我们提供监听键盘弹出的方法,同样,Android系统也没有给我们提供获取软键盘的高度的方法,我们只能自己监听view的高度变化来间接判断键盘是不是弹出了,并通过计算获取软键盘的高度。

目前通用的方法是,由于键盘弹起与隐藏,会使得layout的布局发生变化,通过布局的大小变化触发的事件实现键盘事件的触发。

设置 OnLayoutChangeListener - 推荐

不要忘了设置键盘

android:windowSoftInputMode="stateAlwaysHidden|adjustResize"

使用

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit); int scrollHeight = getWindowManager().getDefaultDisplay().getHeight() / 4;
findViewById(android.R.id.content).addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
if (oldBottom != 0 && bottom != 0) {
int softInputHeight = oldBottom - bottom;//这个就是键盘高度
Log.i("bqt", "【键盘高度】" + Math.abs(softInputHeight)); if (softInputHeight > scrollHeight) {
Toast.makeText(MainActivity.this, "键盘弹起", Toast.LENGTH_SHORT).show();
} else if (Math.abs(softInputHeight) > scrollHeight) {
Toast.makeText(MainActivity.this, "键盘落下", Toast.LENGTH_SHORT).show();
}
}
});
}
}

设置 OnGlobalLayoutListener - 推荐

不要忘了设置键盘

android:windowSoftInputMode="stateAlwaysHidden|adjustResize"

使用

public class MainActivity extends Activity {
private int keyboardHeight; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit); new KeyboardChangeListener(this).setKeyBoardListener((isShow, keyboardCurrentHeight) -> {
Log.i("bqt", "【当前键盘的高度】" + keyboardCurrentHeight);//注意,这里返回的直接是键盘的高度,键盘隐藏时高度为0
if (isShow) {
this.keyboardHeight = keyboardCurrentHeight;
Log.i("bqt", "【键盘的高度】" + keyboardHeight);
Toast.makeText(MainActivity.this, "键盘弹起", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "键盘隐藏", Toast.LENGTH_SHORT).show();
}
});
}
}

KeyboardChangeListener

public class KeyboardChangeListener implements ViewTreeObserver.OnGlobalLayoutListener {
private static final String TAG = "bqt";
private View mContentView;
private int mOriginHeight;
private int mPreHeight;
private KeyBoardListener mKeyBoardListen; public interface KeyBoardListener {
/**
* call back
*
* @param isShow true is show else hidden
* @param keyboardHeight keyboard height
*/
void onKeyboardChange(boolean isShow, int keyboardHeight);
} public void setKeyBoardListener(KeyBoardListener keyBoardListen) {
this.mKeyBoardListen = keyBoardListen;
} public KeyboardChangeListener(Activity contextObj) {
if (contextObj == null) {
Log.i(TAG, "contextObj is null");
return;
}
mContentView = findContentView(contextObj);
if (mContentView != null) {
addContentTreeObserver();
}
} private View findContentView(Activity contextObj) {
return contextObj.findViewById(android.R.id.content);
} private void addContentTreeObserver() {
mContentView.getViewTreeObserver().addOnGlobalLayoutListener(this);
} @Override
public void onGlobalLayout() {
int currHeight = mContentView.getHeight();
if (currHeight == 0) {
Log.i(TAG, "currHeight is 0");
return;
}
boolean hasChange = false;
if (mPreHeight == 0) {
mPreHeight = currHeight;
mOriginHeight = currHeight;
} else {
if (mPreHeight != currHeight) {
hasChange = true;
mPreHeight = currHeight;
} else {
hasChange = false;
}
}
if (hasChange) {
boolean isShow;
int keyboardHeight = 0;
if (mOriginHeight == currHeight) {
//hidden
isShow = false;
} else {
//show
keyboardHeight = mOriginHeight - currHeight;
isShow = true;
} if (mKeyBoardListen != null) {
mKeyBoardListen.onKeyboardChange(isShow, keyboardHeight);
}
}
} @SuppressLint("ObsoleteSdkInt")
public void destroy() {
if (mContentView != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mContentView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
}
}

重写 onSizeChanged - 麻烦,不推荐

不要忘了设置键盘

android:windowSoftInputMode="stateAlwaysHidden|adjustResize"

使用

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit); AdjustSizeRelativeLayout layout = findViewById(R.id.rl_root);
layout.setSoftKeyBoardListener(new AdjustSizeRelativeLayout.SoftkeyBoardListener() {
@Override
public void keyBoardVisable(int move) {
Toast.makeText(MainActivity.this, "键盘弹起,高度:" + move, Toast.LENGTH_SHORT).show();
} @Override
public void keyBoardInvisable(int move) {
Toast.makeText(MainActivity.this, "键盘隐藏,高度:" + move, Toast.LENGTH_SHORT).show();
}
});
}
}

布局

<?xml version="1.0" encoding="utf-8"?>
<com.bqt.test.AdjustSizeRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_root"
android:layout_width="match_parent"
android:layout_height="match_parent"> <EditText
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_alignParentBottom="true"
android:hint="输入交易密码"
android:inputType="text"
android:paddingLeft="5dp"/> </com.bqt.test.AdjustSizeRelativeLayout>

自定义的ViewGroup

public class AdjustSizeRelativeLayout extends RelativeLayout {
public AdjustSizeRelativeLayout(Context context) {
super(context);
} public AdjustSizeRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
} public AdjustSizeRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
} private static final int CHANGE_SIZE = 200; @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Log.i("bqt", "【onSizeChanged】" + w + " " + h + " " + oldw + " " + oldh);
if (oldw == 0 || oldh == 0) return; if (boardListener != null) {
if (h - oldh < -CHANGE_SIZE) {
Log.i("bqt", "键盘弹出" + "change" + w + " " + h + " " + oldw + " " + oldh);
boardListener.keyBoardVisable(Math.abs(h - oldh));
}
if (h - oldh > CHANGE_SIZE) {
Log.i("bqt", "键盘结束" + "change" + w + " " + h + " " + oldw + " " + oldh);
boardListener.keyBoardInvisable(Math.abs(h - oldh));
}
}
} public interface SoftkeyBoardListener {
public void keyBoardVisable(int move); public void keyBoardInvisable(int move);
} private SoftkeyBoardListener boardListener; public void setSoftKeyBoardListener(SoftkeyBoardListener boardListener) {
this.boardListener = boardListener;
}
}

2018-5-21

软键盘 显示隐藏 测量高度 MD的更多相关文章

  1. Android EditText软键盘显示隐藏以及“监听”

    一.写此文章的起因 本人在做类似于微信.易信等这样的聊天软件时,遇到了一个问题.聊天界面最下面一般类似于如图1这样(这里只是显示了最下面部分,可以参考微信等),有输入文字的EditText和表情按钮等 ...

  2. Android软键盘的隐藏显示、事件监听的代码

    把开发过程中重要的一些内容片段做个珍藏,如下资料是关于Android软键盘的隐藏显示.事件监听的内容,应该是对小伙伴们有所用途. public class ResizeLayout extends L ...

  3. Android之Android软键盘的隐藏显示研究

    转自:http://blog.csdn.net/lilu_leo/article/details/6587578 看了很多这类型的文章,这篇文章最有价值,解决了我的烦恼,必须转. Android是一个 ...

  4. android:windowSoftInputMode属性;界面关闭后软键盘不隐藏的解决方法;

    stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置 stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activit ...

  5. iOS中监控软键盘显示或隐藏的可靠方法

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 如果你试图在软键盘的显示或隐藏时去改变的UI界面结构,仅有的方 ...

  6. Android软键盘的隐藏显示研究

    http://winuxxan.blog.51cto.com/2779763/522810 全局推: android:windowSoftInputMode="adjustResize&qu ...

  7. 动态改变tableHeaderView的显示隐藏及高度

    改变tableHeaderView的高度: UIView *headerView = _tableView.tableHeaderView; headerView.height = 10; 当设置高度 ...

  8. iOS 键盘处理(改变键盘为完成键),UITextField键盘显示隐藏,弹出,回弹

    很多时候用到UITextField时,处理键盘是一个很棘手的问题. 问题一:如何隐藏键盘? 方案1.改变键盘右下角的换行(enter)键为完成键,后实现代理方法键盘自动回弹 keyBoardContr ...

  9. Android 软键盘监听事件

    Android软键盘的隐藏显示研究 Android是一个针对触摸屏专门设计的操作系统,当点击编辑框,系统自动为用户弹出软键盘,以便用户进行输入.     那么,弹出软键盘后必然会造成原有布局高度的减少 ...

随机推荐

  1. ueditor百度编辑器上传图片出现后端未配置好,不能正常加载插件

  2. keepalived vip removed with dhcp renewal【原创】

    最近发现公司云平台服务器的vip有丢失的现象,查看keepalived日志 Jun :: lb1 dhclient: DHCPREQUEST of (xid=0x6deab016) Jun :: lb ...

  3. Linux下用jar命令替换war包中的文件【转】

    问题背景:在Linux环境上的weblogic发布war包,有时候只是修改了几个文件,也要上传整个war包,这样很费时间,因此整理了一下Linux环境,更新单个文件的方法. 1.如果要替换的文件直接在 ...

  4. git如何删除远程tag?

    答: 分为两步: 1. 删除本地tag git tag -d tag-name 2. 删除远程tag git push origin :refs/tags/tag-name

  5. Python中__new__和__init__的区别与联系

    __new__ 负责对象的创建而 __init__ 负责对象的初始化. __new__:创建对象时调用,会返回当前对象的一个实例 __init__:创建完对象后调用,对当前对象的一些实例初始化,无返回 ...

  6. 001-guava概述

    一.概述 Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库,例如:集合 [collections] .缓存 [caching] .原生类型支持 [primitives supp ...

  7. 先查询再插入,改为存储过程,java部分入参出参、mybatisxml【我】

    先查询再插入,改为存储过程 create or replace procedure PRO_REVENUE_SI(l_p_cd in Varchar2, l_c_cd in Varchar2, l_p ...

  8. matlab基本函数randperm end数组索引

    一起来学演化计算-matlab基本函数randperm end数组索引 觉得有用的话,欢迎一起讨论相互学习~Follow Me 随机排列 语法 p = randperm(n) p = randperm ...

  9. Spring MVC 为控制器添加通知与处理异常

    与Spring AOP一样,Spring MVC也能够给控制器加入通知,它主要涉及4个注解: •@ControllerAdvice,主要作用于类,用以标识全局性的控制器的拦截器,它将应用于对应的控制器 ...

  10. 一些Python中的二维数组的操作方法

    一些Python中的二维数组的操作方法 这篇文章主要介绍了一些Python中的二维数组的操作方法,是Python学习当中的基础知识,需要的朋友可以参考下 需要在程序中使用二维数组,网上找到一种这样的用 ...