日常开发中,最绕不开的一个控件就是EditText,随之避免不了的则是对其软键盘事故的监听,随着需求的不同对用户输入的软键盘要求也不同,有的场景需要用户输入完毕后,有一个确认按钮,有的场景需要的是回车,有的场景需要用户输入后进入下一项或者搜索,所幸的是,大部分需求场景通过修改原生设置就可满足,只要少少情况下才需要去写自定义键盘。而关于EditText唤起的软键盘中回车的功能可以通过imeOptions的设定来进行相应的设置。
其使用方式仅通过在xml中声明即可:
- <EditText
- ...
- android:imeOptions="actionSend"/>
复制代码 常用属性
如果不特殊声明,右下角按键则为回车键。其常用属性及相应功能设置如下:
属性右下角按键表现及常见应用场景actionGo右下角按键表现“开始”actionSearch右下角表现放大镜,对应搜索功能场景actionSend右下角按键内容为"发送",一般用于即时谈天页面actionNext右下角按键内容为“下一步”或者“下一项”,会跳到下一个EditTextactionDone右下角按键内容为“完成”actionNone无任何提示flagNoExtractUi使软键盘不全屏表现,只占用一部分屏幕,右下角按键为默认回车键在指定imeOptions后,还要添加android:inputType="text"属性。 也可以通过代码去设置对应属性,如下:
- editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
- 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:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <EditText
- android:id="@+id/edt_view"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:imeOptions="actionGo"
- android:inputType="text"/>
- </RelativeLayout>
复制代码 对应在Activity中对其进行事故监听:
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- EditText mEdtView = findViewById(R.id.edt_view);
- mEdtView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
- @Override
- public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
- switch (actionId){
- case EditorInfo.IME_ACTION_DONE:
- Toast.makeText(MainActivity.this, "IME_ACTION_DONE", Toast.LENGTH_SHORT).show();
- break;
- case EditorInfo.IME_ACTION_GO:
- Toast.makeText(MainActivity.this, "IME_ACTION_GO", Toast.LENGTH_SHORT).show();
- break;
- case EditorInfo.IME_ACTION_NEXT:
- Toast.makeText(MainActivity.this, "IME_ACTION_NEXT", Toast.LENGTH_SHORT).show();
- break;
- case EditorInfo.IME_ACTION_NONE:
- Toast.makeText(MainActivity.this, "IME_ACTION_NONE", Toast.LENGTH_SHORT).show();
- break;
- case EditorInfo.IME_ACTION_PREVIOUS:
- Toast.makeText(MainActivity.this, "IME_ACTION_PREVIOUS", Toast.LENGTH_SHORT).show();
- break;
- case EditorInfo.IME_ACTION_SEARCH:
- Toast.makeText(MainActivity.this, "IME_ACTION_SEARCH", Toast.LENGTH_SHORT).show();
- break;
- case EditorInfo.IME_ACTION_SEND:
- Toast.makeText(MainActivity.this, "IME_ACTION_SEND", Toast.LENGTH_SHORT).show();
- break;
- case EditorInfo.IME_ACTION_UNSPECIFIED:
- Toast.makeText(MainActivity.this, "IME_ACTION_UNSPECIFIED", Toast.LENGTH_SHORT).show();
- break;
- default:
- break;
- }
- return true;
- }
- });
- }
复制代码 其对应效果为:

对应吐司也验证了我们代码的运行,我们再在xml中删除对应属性,用代码的形式声明试试。
xml中:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <EditText
- android:id="@+id/edt_view"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:inputType="text"/>
- </RelativeLayout>
复制代码 可见,其余无变更,对应activity的修改则是在设置监听前设置对应属性:
- ...
- mEdtView.setImeOptions(EditorInfo.IME_ACTION_SEND);
- ...
复制代码 其效果为:
可见,代码中设置效果则一样,大概你会疑问,为什么点击右下角按键后还不能收起软键盘,体系中是没有如许主动行为的,需要我们自己来调用以下方法即可:
- InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
- 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企服之家,中国第一个企服评测及商务社交产业平台。 |