安卓EditText点击后虚拟键盘回车变成搜索和事件

举报
第三女神程忆难 发表于 2021/05/26 14:47:23 2021/05/26
【摘要】 安卓EditText点击后虚拟键盘回车变成搜索 一般这种情况用在搜索功能,就如下图, 点击输入框,右下角的回车键变成了搜索键 方法 这个很简单,只需要在输入框的父布局加两句, android:focusable="true"android:focusableInTouchMode="true" EditText中加入 android:imeOptions="...

安卓EditText点击后虚拟键盘回车变成搜索

一般这种情况用在搜索功能,就如下图,

点击输入框,右下角的回车键变成了搜索键

方法

这个很简单,只需要在输入框的父布局加两句,


  
  1. android:focusable="true"
  2. android:focusableInTouchMode="true"

EditText中加入

android:imeOptions="actionSearch"
 

就完事了!

举例

假设一个Toolbar布局,输入框是EditText,那么就在父布局中LinearLayout或者在android.support.v7.widget.Toolbar中加入上面的两句,然后再EditText中加入android:imeOptions="actionSearch"


  
  1. <android.support.v7.widget.Toolbar
  2. android:id="@+id/toolbar"
  3. android:layout_width="match_parent"
  4. android:layout_height="50dp"
  5. android:background="#ff534c"
  6. android:fitsSystemWindows="true"
  7. android:focusable="true" //父布局中加入这一句
  8. android:focusableInTouchMode="true" //父布局中加入这一句
  9. android:scrollbarSize="13sp">
  10. <LinearLayout
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:background="@drawable/head_search"
  14. android:orientation="horizontal"
  15. android:paddingLeft="10dp"
  16. android:paddingRight="1dp">
  17. <Button
  18. android:id="@+id/search"
  19. android:layout_width="22dp"
  20. android:layout_height="22dp"
  21. android:background="@mipmap/search" />
  22. <EditText //这里是输入框
  23. android:id="@+id/search_text"
  24. android:layout_width="130dp"
  25. android:layout_height="28dp"
  26. android:background="@drawable/head_search"
  27. android:hint="@string/search_hint"
  28. android:imeOptions="actionSearch"
  29. android:paddingLeft="5dp"
  30. android:singleLine="true"
  31. android:layout_marginRight="10dp"
  32. android:textColor="#333333"
  33. android:textSize="13sp" />
  34. <Button
  35. android:layout_width="55dp"
  36. android:layout_height="26dp"
  37. android:layout_marginTop="0.3dp"
  38. android:background="@drawable/search_button"
  39. android:text="搜索"
  40. android:textColor="#fff" />
  41. </LinearLayout>
  42. </android.support.v7.widget.Toolbar>

这样就能实现和效果图一样的效果!!!

为EditText添加搜索事件!!!


  
  1. editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  2. @Override
  3. public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  4. //这里写事件,返回为true,即为搜索键的事件
  5. return true;
  6. }
  7. });

 

文章来源: myhub.blog.csdn.net,作者:第三女神程忆难,版权归原作者所有,如需转载,请联系作者。

原文链接:myhub.blog.csdn.net/article/details/83626515

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。