当前位置: 首页 > news >正文

【转载】android软键盘的一些控制

原文地址:http://blog.csdn.net/wang_shaner/article/details/8467688

 

"EditText + Button"  形成一个 "输入+按键响应" 的案例在android编程中是最常见不过的了。

但还有一些细节需要注意:

  1. 在EditText输入后,点击Button进行请求,软键盘应该自行消失
  2. 在EditText输入后,不点击Button进行请求,而是直接点击软键盘上的"回车",那么也应该能够正常响应请求
针对问题1,可以在响应Button的onClick事件中,主动将软键盘隐藏,加入如下代码即可
[java]  view plain copy
 
  1. InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
  2. imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);  
针对问题2,可以在EditText的api doc中找到答案
[plain]  view plain copy
 
  1. public void setOnEditorActionListener (TextView.OnEditorActionListener l)  
  2.   
  3. Set a special listener to be called when an action is performed on the text view. This will be called when the enter key is pressed, or when an action supplied to the IME is selected by the user. Setting this means that the normal hard key event will not insert a newline into the text view, even if it is multi-line; holding down the ALT modifier will, however, allow the user to insert a newline character.  
因此,只需要给EditText设置一个onEditorActionListener就好了,简单示例如下
[java]  view plain copy
 
  1. mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {  
  2.     @Override  
  3.     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  4.         //TODO 这里做"回车"响应处理  
  5.         return true;  
  6.     }  
  7. });  
备注一下:TextView.OnEditorActionListener接口方法onEditorAction方法的第二个参数actionId,其可能的值在 EditorInfo的说明中能够找到。列举如下:
IME_ACTION_DONE
IME_ACTION_GO
IME_ACTION_NEXT
IME_ACTION_NONE
IME_ACTION_PREVIOUS
IME_ACTION_SEARCH
IME_ACTION_SEND
IME_ACTION_UNSPECIFIED
 
 

软键盘的Enter键默认显示的是“完成”文本,我们知道按Enter建表示前置工作已经准备完毕了,要去什么什么啦。比如,在一个搜索中,我们输入要搜索的文本,然后按Enter表示要去搜索了,但是默认的Enter键显示的是“完成”文本,看着不太合适,不符合搜索的语义,如果能显示“搜索”两个字或者显示一个表示搜索的图标多好。事实证明我们的想法是合理的,Android也为我们提供的这样的功能。通过设置android:imeOptions来改变默认的“完成”文本。这里举几个常用的常量值:

  1. actionUnspecified  未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.效果:
  2. actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE 效果:
  3. actionGo 去往,对应常量EditorInfo.IME_ACTION_GO 效果:
  4. actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH 效果: 
  5. actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND 效果:
  6. actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT 效果:
  7. actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE 效果:

 下面已搜索为例,演示一个实例,修改main.xml如下:

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <EditText  
  8.     android:id="@+id/edit_text"    
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"  
  11.     android:imeOptions="actionSearch"/>  
  12. </LinearLayout>  

  修改HelloEditText如下:

Java代码   收藏代码
  1. package com.flysnow;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.KeyEvent;  
  6. import android.widget.EditText;  
  7. import android.widget.TextView;  
  8. import android.widget.Toast;  
  9. import android.widget.TextView.OnEditorActionListener;  
  10.   
  11. public class HelloEditText extends Activity {  
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         EditText editText=(EditText)findViewById(R.id.edit_text);  
  18.         editText.setOnEditorActionListener(new OnEditorActionListener() {  
  19.             @Override  
  20.             public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  21.                 Toast.makeText(HelloEditText.this, String.valueOf(actionId), Toast.LENGTH_SHORT).show();  
  22.                 return false;  
  23.             }  
  24.         });  
  25.     }  
  26. }  

 运行程序,点击回车(也就是搜索图标软键盘按钮)会显示该actionId.我们上面的每一个设置都会对应一个常量,这里的actionId就是那个常量值。 

转载于:https://www.cnblogs.com/LiesSu/p/3835316.html

相关文章:

  • 用element的upload组件实现多图片上传和压缩
  • oracle表空间查询
  • linux 防爆破方法
  • [转载]看我花式绕过校园网计费认证
  • 前嗅ForeSpider采集配置界面介绍
  • Weblogic发布小问题——weblogic.descriptor.DescriptorException: VALIDATION PROBLEMS WERE FOUND
  • POI生成Excel
  • 关于Yii中CGridView关联表字段的filter问题解决方法
  • 78. Subsets
  • 如何识别一个字符串是否Json格式
  • 全能App研发助手!滴滴开源DoraemonKit
  • 码农张的Bug人生 - 初来乍到
  • cesiumjs开发实践(四) 地形介绍
  • Linux的文件管理类命令
  • java android 将小数度数转换为度分秒格式
  • ES6指北【2】—— 箭头函数
  • [原]深入对比数据科学工具箱:Python和R 非结构化数据的结构化
  • 【跃迁之路】【444天】程序员高效学习方法论探索系列(实验阶段201-2018.04.25)...
  • CEF与代理
  • C学习-枚举(九)
  • egg(89)--egg之redis的发布和订阅
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • JavaScript创建对象的四种方式
  • Linux各目录及每个目录的详细介绍
  • Nodejs和JavaWeb协助开发
  • vue 配置sass、scss全局变量
  • 分布式任务队列Celery
  • 码农张的Bug人生 - 见面之礼
  • 前端代码风格自动化系列(二)之Commitlint
  • 收藏好这篇,别再只说“数据劫持”了
  • 写给高年级小学生看的《Bash 指南》
  • 译米田引理
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • AI算硅基生命吗,为什么?
  • 从如何停掉 Promise 链说起
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • (01)ORB-SLAM2源码无死角解析-(56) 闭环线程→计算Sim3:理论推导(1)求解s,t
  • (arch)linux 转换文件编码格式
  • (solr系列:一)使用tomcat部署solr服务
  • (附源码)计算机毕业设计ssm高校《大学语文》课程作业在线管理系统
  • (学习日记)2024.01.09
  • (转)visual stdio 书签功能介绍
  • .java 9 找不到符号_java找不到符号
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • .Net CoreRabbitMQ消息存储可靠机制
  • .net 获取url的方法
  • .Net 垃圾回收机制原理(二)
  • .net 逐行读取大文本文件_如何使用 Java 灵活读取 Excel 内容 ?
  • .NET/C# 在 64 位进程中读取 32 位进程重定向后的注册表
  • .net开源工作流引擎ccflow表单数据返回值Pop分组模式和表格模式对比
  • .NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
  • .NET中GET与SET的用法
  • .xml 下拉列表_RecyclerView嵌套recyclerview实现二级下拉列表,包含自定义IOS对话框...
  • @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
  • [2015][note]基于薄向列液晶层的可调谐THz fishnet超材料快速开关——