【问题标题】:How to force keyboard to show/hide? [duplicate]如何强制键盘显示/隐藏? [复制]
【发布时间】:2011-12-28 18:10:54
【问题描述】:

我在给 LinearLayout 充气并调用 setContentView 后尝试显示键盘,如下所示:

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(etContent, InputMethodManager.SHOW_FORCED);
getContent.requestFocus();

没有用。我也试过这个:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

但它也没有工作。如何强制键盘显示/隐藏?我做错了什么?

【问题讨论】:

标签: android keyboard


【解决方案1】:

这应该可以工作

public class KeyBoard {

    public static void toggle(Activity activity){
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (imm.isActive()){
            imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); // hide
        } else {
            imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY); // show
        }
    }//end method
}//end class

【讨论】:

  • 不错的答案仍然值得:)
  • 节省了我的时间...它的工作...
【解决方案2】:

this link 很清楚隐藏软键盘。 为了展示它,您可以使用 hack - 在布局中的任何位置创建 EditText,layout_width 和 layout_height=0dip,并在 onCreate 中执行

yourEditText.requestFocus();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-12
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 1970-01-01
    • 2020-09-11
    相关资源
    最近更新 更多