[英]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.