我没法弄清楚这一点。 有些应用程序有一个EditText(文本框),当你触摸它并调出屏幕键盘时,键盘上有一个“搜索”按钮而不是一个回车键。 java
我想实现这个。 如何实现“搜索”按钮并检测按“搜索”按钮? android
编辑 :找到如何实现搜索按钮; 在XML中, android:imeOptions="actionSearch"
或在Java中, EditTextSample.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
。 可是如何处理用户按下“搜索”按钮? 它与android:imeActionId
有什么关系android:imeActionId
? 编辑器
在布局中设置要搜索的输入法选项。 ide
<EditText android:imeOptions="actionSearch" android:inputType="text" />
在java中添加编辑器动做监听器。 布局
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { performSearch(); return true; } return false; } });
在xml
文件中, imeOptions="actionSearch"
和inputType="text"
, maxLines="1"
: spa
<EditText android:id="@+id/search_box" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/search" android:imeOptions="actionSearch" android:inputType="text" android:maxLines="1" />
用户单击搜索时隐藏键盘。 除了Robby Pond回答 code
private void performSearch() { editText.clearFocus(); InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0); //...perform search }
在科特林 orm
evLoginPassword.setOnEditorActionListener { _, actionId, _ -> if (actionId == EditorInfo.IME_ACTION_DONE) { doTheLoginWork() } true }
部分Xml代码 xml
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginTop="8dp" android:paddingLeft="24dp" android:paddingRight="24dp"> <EditText android:id="@+id/evLoginUserEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/email" android:inputType="textEmailAddress" android:textColor="@color/black_54_percent" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginTop="8dp" android:paddingLeft="24dp" android:paddingRight="24dp"> <EditText android:id="@+id/evLoginPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/password" android:inputType="textPassword" android:imeOptions="actionDone" android:textColor="@color/black_54_percent" /> </android.support.design.widget.TextInputLayout> </LinearLayout>
经过XML: get
<EditText android:id="@+id/search_edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/search" android:imeOptions="actionSearch" android:inputType="text" />
经过Java:
editText.clearFocus(); InputMethodManager in = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);