仿京东,在打开搜索页面时,获取输入框焦点,自动弹出输入法的软键盘,并且修改软键盘右下角按钮为“搜索”,同时响应搜索按钮的点击事件。

1、设置EditText布局

加上属性:android:imeOptions="actionSearch",这个属性的值为:actionSearch:
搜索、actionSend:发送、actionNext: 下一项
<EditText android:id="@+id/jd2b_search_et"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@color/transparent" android:hint="搜索您想要的商品"
android:imeOptions="actionSearch" android:inputType="text"
android:textColor="#666666" android:textColorHint="#cccccc"/>
2、获取EditText焦点和弹出输入法

这里一定要设置延迟,不然会导致弹出输入法失效。
new Handler().postDelayed(new Runnable(){ public void run() {
edSearch.requestFocus(); InputMethodManager imm = (InputMethodManager)
context.getSystemService(INPUT_METHOD_SERVICE); imm.showSoftInput(edSearch,
InputMethodManager.SHOW_IMPLICIT); } }, 100); //0.1秒
3、设置软键盘按钮监听
edSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override public boolean onEditorAction(TextView textView, int actionId,
KeyEvent keyEvent) { // 按下按钮,这里和xml文件中的EditText中属性imeOptions对应; if (actionId ==
EditorInfo.IME_ACTION_SEARCH) { setResult(edSearch.getText().toString());
return false; } return true;//返回true,保留软键盘;false,隐藏软键盘 } });
 

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信