Android 更改软键盘Enter键为搜索
2016-06-28 11:14
399 查看
项目当中 有关键词搜索商品,但是 又不想单独做一个按钮 那么就更改一下软键盘的enter键 来帮助实现这个需求
一些注意事项 全部写在 注释当中这里不在赘述
xml代码
java代码
一些注意事项 全部写在 注释当中这里不在赘述
xml代码
<EditText android:textColorHint="#CCCCCC" android:background="@drawable/drouble" android:id="@+id/search" android:hint="搜索需要的商品..." android:textSize="13dip" android:layout_width="12dp" android:layout_weight="4" android:layout_height="match_parent" android:imeOptions="actionSearch" android:inputType="text" android:singleLine="true" />
/*****/
android:imeOptions="actionSearch" android:inputType="text" android:singleLine="true" 这三条属性必须加上
java代码
// http://my.oschina.net/u/2444750/blog/523209 更改软件盘 Enter键 属性 editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { Toast.makeText(getApplicationContext(), "换行键已更改", Toast.LENGTH_SHORT).show(); ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow( MainActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } return false; } }); //点击软键盘搜索按钮后隐藏掉软键盘 final InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); editText.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) { manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } return false; } }); // .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
相关文章推荐
- Android开发中Parcelable接口用法
- Windows下NDK开发环境的搭建(Cygwin+Android-SDK+NDK)(转)
- Android(六)----页面跳转和数据传递
- Android Studio 环境搭建
- Android Material Design系列之FloatingActionButton和Snackbar
- Android的任务栈简介、AndroidMainifest启动模式、Intent Flag启动模式
- Xamarin中打开别人项目找不到android.jar文件
- Android Studio目录结构
- 手机usb有线上网
- Android Studio 常用快捷键大全
- 新建安卓项目运行出现AndroidMainFest.xml file missing!
- Android调试工具
- android应用流量统计小谈
- ViewGroup 的事件分发机制
- Fresco 0.10.0 混淆后 不能显示GIF图片(Proguard)
- Android Studio Gradle 缓存文件夹设置
- Android DrawerLayout 高仿QQ5.2双向侧滑菜单
- Android安全扫描检查
- “/dev/kvm not found ”error on windows in android studio
- Android studio实现get和set的快捷键