EditText 监听回车
使用EditText时,有时候我们会需要监听输入的回车,以做出一些操作。 或者需要把回车变成“搜索”,“发送”或“完成”等等。
EditText 为我们提供了一个属性 imeOptions 用来替换软键盘中 enter 键的外观,如actionGo 会使外观变成“前往”。 需要同时设置android:inputType="text"。
<EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:imeOptions="actionGo"android:inputType="text"/>常用的几个属性以及替换的文本外观:
| 属性 | 说明 | 对应静态变量 |
|---|---|---|
| actionUnspecified | 未指定 | EditorInfo.IME_ACTION_UNSPECIFIED |
| actionNone | 动作 | EditorInfo.IME_ACTION_NONE |
| actionGo | 去往 | EditorInfo.IME_ACTION_GO |
| actionSearch | 搜索 | EditorInfo.IME_ACTION_SEARCH |
| actionSend | 发送 | EditorInfo.IME_ACTION_SEND |
| actionNext | 下一项 | EditorInfo.IME_ACTION_NEXT |
| actionDone | 完成 | EditorInfo.IME_ACTION_DONE |
设置的方法可以在布局文件中设置android:imeOptions="actionNext"或者在代码中mUserEdit.setImeOptions(EditorInfo.IME_ACTION_NEXT);
接下来设置回车按键的监听事件setOnEditorActionListener
et1.setOnEditorActionListener(mOnEditorActionListener);// ......privateTextView.OnEditorActionListener mOnEditorActionListener=newTextView.OnEditorActionListener(){@OverridepublicbooleanonEditorAction(TextView v,int actionId,KeyEvent event){Log.d(TAG,"actionId: "+actionId);Log.d(TAG,"event: "+event);returnfalse;}};如果 onEditorAction 返回 true,表示消费调这个事件。
上面的 actionId 对应的是android.view.inputmethod.EditorInfo中的常量。
publicstaticfinal intIME_ACTION_UNSPECIFIED=0x00000000;publicstaticfinal intIME_ACTION_NONE=0x00000001;publicstaticfinal intIME_ACTION_GO=0x00000002;publicstaticfinal intIME_ACTION_SEARCH=0x00000003;publicstaticfinal intIME_ACTION_SEND=0x00000004;publicstaticfinal intIME_ACTION_NEXT=0x00000005;publicstaticfinal intIME_ACTION_DONE=0x00000006;publicstaticfinal intIME_ACTION_PREVIOUS=0x00000007;EditText 光标移动与选择
主要介绍 setSelection 方法。
setSelection有:
- setSelection(int start, int stop) 选择范围
- setSelection(int index) 把光标移动到指定位置
例:假设有EditText,变量名为mEt1。
- 把光标移动到最前mEt1.setSelection(0);mEt1.setSelection(mEt1.getText().length());mEt1.setSelection(mEt1.getSelectionEnd() + 1);mEt1.setSelection(mEt1.getSelectionEnd() - 1);要注意的是,如果传入的index超出了text的范围,会报
java.lang.IndexOutOfBoundsException因此在实际工程中,需要判断传入的位置是否在EditText已有内容的长度范围内。 - 把光标移动到最后
- 光标右移一位
- 光标左移一位
- 全选当前输入的textmEt1.setSelection(0, mEt1.getText().length());
监听输入内容
代码中动态限制输入长度
使用TextWatcher
mQueryEt.addTextChangedListener(newTextWatcher(){@OverridepublicvoidbeforeTextChanged(CharSequence s,int start,int count,int after){}@OverridepublicvoidonTextChanged(CharSequence s,int start,int before,int count){}@OverridepublicvoidafterTextChanged(Editable s){// 如果EditText中的数据不为空,且长度大于指定的最大长度if(!TextUtils.isEmpty(s)&&s.length()>15){// 删除指定长度之后的数据s.delete(15,s.length()-1);}}});最后分享一份
【腾讯技术团队出品】Android零基础入门到精通,Android Studio安装教程+全套安卓基础教程
Android编程入门教程
Java语言基础从入门到熟悉
Kotlin语言基础从入门到熟悉
Android 技术栈从入门到熟悉
Android Jetpack 全家桶全面学习
对于新手来说可能安装Android Studio存在一定困难你可以看着以下视频,一步步的跟着学习安装运行
Android Studio 安装教程
有了Java阶段的学习,这一阶段建议以视频学习为主辅以图书查漏补缺。如果以图书为主,可以根据图书讲解敲代码,辅以教学视频查漏补缺。遇到问题可以去百度,入门的问题一般会有很多人遇到,并且给出比较好的解答。
需要掌握基本知识点,比如四大组件如何使用、如何创建Service、如何进行布局、简单的自定义View、动画、网络通信等常见技术。
全套零基础教程已经为你们准备好了,需要的可以添加下方二维码免费领取
全套安卓基础教程