Android EditText关于imeOptions的设置和相应

打印 上一主题 下一主题

主题 1052|帖子 1052|积分 3156


日常开发中,最绕不开的一个控件就是EditText,随之避免不了的则是对其软键盘事故的监听,随着需求的不同对用户输入的软键盘要求也不同,有的场景需要用户输入完毕后,有一个确认按钮,有的场景需要的是回车,有的场景需要用户输入后进入下一项或者搜索,所幸的是,大部分需求场景通过修改原生设置就可满足,只要少少情况下才需要去写自定义键盘。而关于EditText唤起的软键盘中回车的功能可以通过imeOptions的设定来进行相应的设置。
其使用方式仅通过在xml中声明即可:
  1. <EditText
  2.             ...
  3.             android:imeOptions="actionSend"/>
复制代码
常用属性

如果不特殊声明,右下角按键则为回车键。其常用属性及相应功能设置如下:
属性右下角按键表现及常见应用场景actionGo右下角按键表现“开始”actionSearch右下角表现放大镜,对应搜索功能场景actionSend右下角按键内容为"发送",一般用于即时谈天页面actionNext右下角按键内容为“下一步”或者“下一项”,会跳到下一个EditTextactionDone右下角按键内容为“完成”actionNone无任何提示flagNoExtractUi使软键盘不全屏表现,只占用一部分屏幕,右下角按键为默认回车键在指定imeOptions后,还要添加android:inputType="text"属性。 也可以通过代码去设置对应属性,如下:
  1. editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
  2. editText.setImeOptions(EditorInfo.IME_ACTION_...);
复制代码
其属性与代码中设置的常量关系为:
属性对应常量actionGoEditorInfo.IME_ACTION_GOactionSearchEditorInfo.IME_ACTION_SEARCHactionSendEditorInfo.IME_ACTION_SENDactionNextEditorInfo.IME_ACTION_NEXTactionDoneEditorInfo.IME_ACTION_DONEactionNoneEditorInfo.IME_ACTION_NONEactionUnspecified(未指定)EditorInfo.IME_ACTION_UNSPECIFIED 监听

对应的EditText可以设置相应的监听,editText.setOnEditorActionListener,在监听的onEditorAction()中通过返回的actionId参数来判断触发的对应事故。比方以下示例:
xml中简单设置一个EditText:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     tools:context=".MainActivity">
  7.     <EditText
  8.         android:id="@+id/edt_view"
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:layout_centerInParent="true"
  12.         android:imeOptions="actionGo"
  13.         android:inputType="text"/>
  14. </RelativeLayout>
复制代码
对应在Activity中对其进行事故监听:
  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3.     super.onCreate(savedInstanceState);
  4.     setContentView(R.layout.activity_main);
  5.     EditText mEdtView = findViewById(R.id.edt_view);
  6.     mEdtView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  7.         @Override
  8.         public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  9.             switch (actionId){
  10.                 case EditorInfo.IME_ACTION_DONE:
  11.                     Toast.makeText(MainActivity.this, "IME_ACTION_DONE", Toast.LENGTH_SHORT).show();
  12.                     break;
  13.                 case EditorInfo.IME_ACTION_GO:
  14.                     Toast.makeText(MainActivity.this, "IME_ACTION_GO", Toast.LENGTH_SHORT).show();
  15.                     break;
  16.                 case EditorInfo.IME_ACTION_NEXT:
  17.                     Toast.makeText(MainActivity.this, "IME_ACTION_NEXT", Toast.LENGTH_SHORT).show();
  18.                     break;
  19.                 case EditorInfo.IME_ACTION_NONE:
  20.                     Toast.makeText(MainActivity.this, "IME_ACTION_NONE", Toast.LENGTH_SHORT).show();
  21.                     break;
  22.                 case EditorInfo.IME_ACTION_PREVIOUS:
  23.                     Toast.makeText(MainActivity.this, "IME_ACTION_PREVIOUS", Toast.LENGTH_SHORT).show();
  24.                     break;
  25.                 case EditorInfo.IME_ACTION_SEARCH:
  26.                     Toast.makeText(MainActivity.this, "IME_ACTION_SEARCH", Toast.LENGTH_SHORT).show();
  27.                     break;
  28.                 case EditorInfo.IME_ACTION_SEND:
  29.                     Toast.makeText(MainActivity.this, "IME_ACTION_SEND", Toast.LENGTH_SHORT).show();
  30.                     break;
  31.                 case EditorInfo.IME_ACTION_UNSPECIFIED:
  32.                     Toast.makeText(MainActivity.this, "IME_ACTION_UNSPECIFIED", Toast.LENGTH_SHORT).show();
  33.                     break;
  34.                 default:
  35.                     break;
  36.             }
  37.             return true;
  38.         }
  39.     });
  40. }
复制代码
其对应效果为:

对应吐司也验证了我们代码的运行,我们再在xml中删除对应属性,用代码的形式声明试试。
xml中:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     tools:context=".MainActivity">
  7.     <EditText
  8.         android:id="@+id/edt_view"
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:layout_centerInParent="true"
  12.         android:inputType="text"/>
  13. </RelativeLayout>
复制代码
可见,其余无变更,对应activity的修改则是在设置监听前设置对应属性:
  1. ...
  2. mEdtView.setImeOptions(EditorInfo.IME_ACTION_SEND);
  3. ...
复制代码
其效果为:

可见,代码中设置效果则一样,大概你会疑问,为什么点击右下角按键后还不能收起软键盘,体系中是没有如许主动行为的,需要我们自己来调用以下方法即可:
  1. InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
  2. imm.hideSoftInputFromWindow(context.getWindow().getDecorView().getWindowToken(), 0);
复制代码
重复相应问题

这里需要注意的是,onEditorAction中,如果返回的是false,则onEditorAction中的代码大概会调用两次,原因不难理解,体系会首先判断用户实现的方法onEditorActionListener.onEditorAction(this, actionCode, null)的返回值,一旦返回true,会立即return,因此体系的处理被直接跳过。
设置无效问题

当设置了android:maxLines=“1” 属性时,有大概出现设置无效问题,这里要改为android:singleLine="true"此属性即可。当然,有大概个别的机型还有其他适配问题,好比三星等等,有遇见的朋友可以留言互相交流。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

络腮胡菲菲

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表