Ric*_*ket 359 android android-softkeyboard
我无法弄清楚这一点.有些应用程序有一个EditText(文本框),当你触摸它并调出屏幕键盘时,键盘上有一个"搜索"按钮而不是一个回车键.
我想实现这个.如何实现"搜索"按钮并检测按"搜索"按钮?
编辑:找到如何实现搜索按钮; 用XML android:imeOptions="actionSearch"或Java EditTextSample.setImeOptions(EditorInfo.IME_ACTION_SEARCH);.但是如何处理用户按下"搜索"按钮?它与之有关android:imeActionId吗?
Rob*_*ond 869
在布局中设置要搜索的输入法选项.
<EditText
android:imeOptions="actionSearch"
android:inputType="text" />
Run Code Online (Sandbox Code Playgroud)
在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;
}
});
Run Code Online (Sandbox Code Playgroud)
小智 15
用户单击搜索时隐藏键盘.除了Robby Pond回答
private void performSearch() {
editText.clearFocus();
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
//...perform search
}
Run Code Online (Sandbox Code Playgroud)
Sha*_*aon 10
在科特林
evLoginPassword.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
doTheLoginWork()
}
true
}
Run Code Online (Sandbox Code Playgroud)
部分 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>
Run Code Online (Sandbox Code Playgroud)
在xml文件中,把imeOptions="actionSearch"和inputType="text",maxLines="1":
<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" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
158888 次 |
| 最近记录: |