简体   繁体   English

按下按钮后显示键盘

[英]Show keyboard after pressing a button

I have clear button to clear EditText. 我有清除按钮以清除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" />

This method clears EditText: 此方法清除EditText:

    public void clearEtSearch(View v) {
    etSearch.setText("");
    etSearch.requestFocus();
    showKeyboard(etSearch);
}

I have taken code below from hiding and changed to show keyboard, but it is not working 我从隐藏中获取了以下代码,并更改为显示键盘,但是它不起作用

    private void showKeyboard(View view) {
    InputMethodManager manager = (InputMethodManager) view.getContext()
            .getSystemService(INPUT_METHOD_SERVICE);
    if (manager != null)
        manager.showSoftInputFromInputMethod(view.getWindowToken(), 0);
}

What I am doing wrong? 我做错了什么? Please give suggestions to correct my code. 请提出建议以更正我的代码。

I'm not sure but you can try to use Context.INPUT_METHOD_SERVICE instead of just INPUT_METHOD_SERVICE . 我不确定,但是您可以尝试使用Context.INPUT_METHOD_SERVICE而不是仅使用INPUT_METHOD_SERVICE Some are Forcing the Soft Keyboard open question for more. 有些人正在强迫“软键盘”打开问题以获取更多信息。

You can also see How to show soft-keyboard when edittext is focused See if following works: 您还可以查看在聚焦edittext时如何显示软键盘。

public void clearEtSearch(View v) {
    etSearch.setText("");
    etSearch.requestFocus();

    InputMethodManager manager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);    
    manager.showSoftInputFromInputMethod(etSearch.getWindowToken(), 0);

}

Depending on your need you may try different constants of InputMethodManager like following: 根据您的需要,您可以尝试不同的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);
}

or 要么

public void clearEtSearch(View v) {
    etSearch.setText("");
    etSearch.requestFocus();
    InputMethodManager manager= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    manager.showSoftInput(etSearch, InputMethodManager.SHOW_FORCED);
}

I haven't tried the code right not so not sure which one would work. 我没有尝试正确的代码,所以不确定是否可以使用。 See There are many related questions. 请参阅有许多相关问题。 Hope it works well for you. 希望它对您有用。

Use this method and enjoy 使用这种方法享受

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM