ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android_基础_editText的setOnEditorActionListener方法的使用

2021-11-29 12:01:48  阅读:285  来源: 互联网

标签:IME editText setOnEditorActionListener EditorInfo 软键盘 EditText ACTION android


转载自: 点击查看原文原文

控件EditText的setOnEditorActionListener方法的使用

说明:需要注意的是 setOnEditorActionListener这个方法,并不是在我们点击EditText的时候触发,也不是在我们对EditText进行编辑时触发,而是在我们编辑完之后点击软键盘上的各种键才会触发。

因为通过布局文件中的imeOptions可以控制软件盘右下角的按钮显示为不同按钮。所以和EditorInfo搭配起来可以实现各种软键盘的功能。

各种属性对应:

  • imeOptions=”actionUnspecified” –> EditorInfo.IME_ACTION_UNSPECIFIED
  • imeOptions=”actionNone” –> EditorInfo.IME_ACTION_NONE
  • imeOptions=”actionGo” –> EditorInfo.IME_ACTION_GO
  • imeOptions=”actionSearch” –> EditorInfo.IME_ACTION_SEARCH
  • imeOptions=”actionSend” –> EditorInfo.IME_ACTION_SEND
  • imeOptions=”actionNext” –> EditorInfo.IME_ACTION_NEXT
  • imeOptions=”actionDone” –> EditorInfo.IME_ACTION_DONE

用法

  • 布局中定义一个EditText控件
     <EditText
            android:id="@+id/ET_phonenumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/buttonAdd"
            android:hint="@string/enter_new_note" 
            android:imeOptions="actionDone"             // 这里和onEditorAction中actionId对应。
            android:inputType="text"/>
  • 定义一个可编辑的editText控件
    EditText ET_phone = (EditText) findViewById(R.id.ET_phonenumber);
  • 添加setOnEditorActionListener方法
    ET_phone.setOnEditorActionListener(new OnEditorActionListener() {  
                @Override  
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
                   if (actionId == EditorInfo.IME_ACTION_DONE) {   // 按下完成按钮,这里和上面imeOptions对应
                    text.setText("Editing EditorInfo.IME_ACTION_DONE");  
                    return false;   //返回true,保留软键盘。false,隐藏软键盘
                    }
                }  
            });  

注意: 返回false表示点击后,隐藏软键盘。返回true表示保留软键盘。

标签:IME,editText,setOnEditorActionListener,EditorInfo,软键盘,EditText,ACTION,android
来源: https://blog.csdn.net/qq_42420293/article/details/121606503

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有