[英]Show keyboard after pressing a button
我有清除按钮以清除EditText。
<Button
android:id="@+id/bClearText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="10dp"
android:onClick="clearEtSearch"
android:background="@drawable/delete" />
此方法清除EditText:
public void clearEtSearch(View v) {
etSearch.setText("");
etSearch.requestFocus();
showKeyboard(etSearch);
}
我从隐藏中获取了以下代码,并更改为显示键盘,但是它不起作用
private void showKeyboard(View view) {
InputMethodManager manager = (InputMethodManager) view.getContext()
.getSystemService(INPUT_METHOD_SERVICE);
if (manager != null)
manager.showSoftInputFromInputMethod(view.getWindowToken(), 0);
}
我做错了什么? 请提出建议以更正我的代码。
我不确定,但是您可以尝试使用Context.INPUT_METHOD_SERVICE
而不是仅使用INPUT_METHOD_SERVICE
。 有些人正在强迫“软键盘”打开问题以获取更多信息。
您还可以查看在聚焦edittext时如何显示软键盘。
public void clearEtSearch(View v) {
etSearch.setText("");
etSearch.requestFocus();
InputMethodManager manager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInputFromInputMethod(etSearch.getWindowToken(), 0);
}
根据您的需要,您可以尝试不同的InputMethodManager常量,如下所示:
public void clearEtSearch(View v) {
etSearch.setText("");
etSearch.requestFocus();
InputMethodManager manager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
manager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
要么
public void clearEtSearch(View v) {
etSearch.setText("");
etSearch.requestFocus();
InputMethodManager manager= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInput(etSearch, InputMethodManager.SHOW_FORCED);
}
我没有尝试正确的代码,所以不确定是否可以使用。 请参阅有许多相关问题。 希望它对您有用。
使用这种方法享受
public void showSoftKeyboard() {
try {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.SHOW_FORCED, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.