Android学习之监听软键盘的弹起关闭
来源:互联网 发布:淘宝的正品保证有用吗 编辑:程序博客网 时间:2024/05/21 16:27
今天在项目中遇见需要监听软键盘的弹起与关闭功能,因此在网上查找了一下资料,并实践成功,因为回家以记之。
分析:
1.软件盘弹起与关闭需要顶起布局。
【A】stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置
【B】stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示
【C】stateHidden:用户选择activity时,软键盘总是被隐藏
【D】stateAlwaysHidden:当该Activity主窗口获取焦点时,软键盘也总是被隐藏的
【E】stateVisible:软键盘通常是可见的
【F】stateAlwaysVisible:用户选择activity时,软键盘总是显示的状态
【G】adjustUnspecified:默认设置,通常由系统自行决定是隐藏还是显示
【H】adjustResize:该Activity总是调整屏幕的大小以便留出软键盘的空间
【I】adjustPan:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分
要实现布局被顶起,需要在清单文件下做如下设置。
<activity android:name="com.bear.softkeyboardlistener.MainActivity" android:label="@string/app_name" android:windowSoftInputMode="stateAlwaysHidden|adjustResize" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
2.监听软键盘顶起布局的距离来判断弹起与关闭。
import com.bear.bearbroadcastreceiver.R;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnLayoutChangeListener;import android.widget.Toast;public class MainActivity extends Activity implements OnLayoutChangeListener{ //Activity最外层的Layout视图 private RelativeLayout activityRootView; //屏幕高度 private int screenHeight = 0; //软件盘弹起后所占高度阀值 private int keyHeight = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); activityRootView = findViewById(R.id.root_layout); //获取屏幕高度 screenHeight = this.getWindowManager().getDefaultDisplay().getHeight(); //阀值设置为屏幕高度的1/3 keyHeight = screenHeight/3; } @Override protected void onResume() { super.onResume(); //添加layout大小发生改变监听器 activityRootView.addOnLayoutChangeListener(this); } @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { //old是改变前的左上右下坐标点值,没有old的是改变后的左上右下坐标点值// System.out.println(oldLeft + " " + oldTop +" " + oldRight + " " + oldBottom);// System.out.println(left + " " + top +" " + right + " " + bottom); //现在认为只要控件将Activity向上推的高度超过了1/3屏幕高,就认为软键盘弹起 if(oldBottom != 0 && bottom != 0 &&(oldBottom - bottom > keyHeight)){ Toast.makeText(MainActivity.this, "监听到软键盘弹起...", Toast.LENGTH_SHORT).show(); }else if(oldBottom != 0 && bottom != 0 &&(bottom - oldBottom > keyHeight)){ Toast.makeText(MainActivity.this, "监听到软件盘关闭...", Toast.LENGTH_SHORT).show(); } }
这里activityRootView是根布局,可以根据情况设置,通过OnLayoutChangeListener来监听布局位移。
if(oldBottom != 0 && bottom != 0 &&(oldBottom - bottom > keyHeight))
底部起始位置不为零,移动后的位置不为零,位移大于屏幕三分之一高度,即可视为软件盘弹起。
很好理解,就不贴代码了,感谢博主
http://blog.csdn.net/bear_huangzhen/article/details/45896333
在给大家推荐一个软件盘工具类,来控制软键盘的弹起与隐藏。
package com.zhy.utils;import android.content.Context;import android.view.inputmethod.InputMethodManager;import android.widget.EditText;/** * 打开或关闭软键盘 * * @author zhy * */public class KeyBoardUtils{ /** * 打卡软键盘 * * @param mEditText * 输入框 * @param mContext * 上下文 */ public static void openKeybord(EditText mEditText, Context mContext) { InputMethodManager imm = (InputMethodManager) mContext .getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); } /** * 关闭软键盘 * * @param mEditText * 输入框 * @param mContext * 上下文 */ public static void closeKeybord(EditText mEditText, Context mContext) { InputMethodManager imm = (InputMethodManager) mContext .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0); }}
- Android学习之监听软键盘的弹起关闭
- Android之监听手机软键盘弹起与关闭
- Android之监听手机软键盘弹起与关闭
- Android之监听手机软键盘弹起与关闭
- Android之监听手机软键盘弹起与关闭
- Android监听手机软键盘的弹起和关闭
- 监听软键盘 弹起 关闭
- Android 软键盘弹起隐藏的监听
- 监听手机软键盘弹起与关闭
- Android 监听home键,及软键盘的弹起和关闭
- android 监听软键盘弹起和关闭状态与点击空白区域软键盘隐藏
- Android监听软键盘的弹起和隐藏
- android软键盘的监听和手动弹起
- android 软键盘弹起监听事件
- Android监听软键盘弹起+收回
- Android监听软键盘弹起隐藏
- 监听软键盘的弹起与隐藏
- 监听软键盘的弹起和收起
- python学习笔记-语法
- leetcode138: Copy List with Random Pointer
- Java中的继承
- 聊聊“尾递归优化“简介拷贝构造&拷贝赋值
- 分享一个牛逼的chrome插件--Postman
- Android学习之监听软键盘的弹起关闭
- android关于调用联系人信息的问题
- html 缓存公共数据数据 银行卡校验 身份证校验
- HDU4848WoW
- maven常见开发配置spring,hibernate,structs,jdbc,log4j,mybatis
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- Servlet——过滤器
- findViewById(R.id.xxx)返回Null
- 设计模式之代理模式