
Android 开发学习进程0.29 软键盘使用问题
发布日期:2021-05-09 00:48:52
浏览次数:16
分类:技术文章
本文共 2132 字,大约阅读时间需要 7 分钟。
软键盘使用问题
软键盘在edittext获取焦点后自动弹出,需要设置监听:
etActsearchInput.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { // close soft keyboard InputMethodManager inputMethodManager = (InputMethodManager) v.getContext() .getSystemService(Context.INPUT_METHOD_SERVICE); if (inputMethodManager.isActive()) { inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0); } search(etActsearchInput.getText().toString()); return true; } return false; } });
以上实现了监听 软键盘的确定键,同时 edittext布局文件同样需设置
android:singleLine="true"android:imeOptions="actionSearch"
imeOptions属性决定了软键盘右下角按键文字,属性包括搜索、确定、换行。同时由edit源码知:调用软键盘与关闭软键盘是相似的,需要分两步:
1、获取InputMethodManager 实例2、调用showSoftInput(xx)非在editview中使用软键盘时,如button设置Button.setFocusableInTouchMode(true)设置弹出软键盘时activity布局是否被顶上去getWindow().getAttributes().softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
或是在manifest文件中设置android:windowSoftInputMode="adjustResize"
部分app有 activity的window不往上移动,而editview移动至软键盘上,这样实现的原理是,需要设置属性android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
同时计算软键盘高度,移动editview坐标。点击外部隐藏软键盘: @Override public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { //获取焦点View View v = getCurrentFocus(); if (isFocusEditText(v, hideSoftByEditViewIds())) { //如果焦点在editText上 //隐藏键盘 KeyBoardUtils.hideInputForce(this); //清除焦点 clearViewFocus(v, hideSoftByEditViewIds()); } } return super.dispatchTouchEvent(ev); }
可以选择id过滤的函数:
@Override public int[] hideSoftByEditViewIds() { int[] ids = {R.id.et_phone, R.id.et_check_code, R.id.et_city_code}; return ids; }
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月21日 05时21分02秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
labuladong算法学习
2023-01-30
labview如何把A数组的第一个数据插入到B数组的最后一个元素
2023-01-30
Lake Counting
2023-01-30
lambda 与列表理解性能
2023-01-30
Lambda 实现超强排序
2023-01-30
lambda表达式
2023-01-30
lambda表达式与匿名内部类与双冒号(::)
2023-01-30
Lambda表达式入门,看这篇就够了!
2023-01-30
Lammp安装过程
2023-01-30
lamp 一键安装
2023-01-30
Lamp(Fpm-Php)基本配置
2023-01-30
LAMP下添加支持openssl的主机
2023-01-30
LAMP与LNMP架构详解
2023-01-30
LAMP架构部署实战(附LAMP源码包和CRUD测试Web网站)
2023-01-30
LAMP网站平台搭建
2023-01-30
LangChain:链接语言与智能的未来
2023-01-30
LangFuse的简单介绍
2023-01-30