【问题标题】:When keyboard active then touch screen anywhere, close keyboard当键盘处于活动状态,然后在任何地方触摸屏幕,关闭键盘
【发布时间】:2014-12-30 05:20:34
【问题描述】:

我认为我无法使用正确的词来找到我想要的代码,因为我认为这是可行的。

我只想从 android 关闭键盘视图,如果我按下键以外的任何地方。 (实际上在键盘上方...)

例如当我在 EditView 中时,完成编辑。

【问题讨论】:

标签: java android keyboard touch


【解决方案1】:

这是你应该用来隐藏软键盘的功能

public static void hideSoftKeyboard(Context context){
    InputMethodManager inputMethodManager = (InputMethodManager) context
            .getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(context.getCurrentFocus()
            .getWindowToken(), 0);
}

【讨论】:

  • 我应该在哪里调用这个方法?
  • 您必须在您想要隐藏键盘的活动中的视图(可能是父视图)上应用某种触摸侦听器(尤其是单点)。显示您的布局会告诉你的
【解决方案2】:
EditText myEditText = (EditText) findViewById(R.id.myEditText);  
InputMethodManager imm = (InputMethodManager)getSystemService(
      Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

【讨论】:

  • 似乎不适合我。当我单击 EditView 然后单击屏幕上的任意位置时,键盘仍然停留在那里。
【解决方案3】:
EditText myEditText = (EditText) findViewById(R.id.myEditText); 
myEditText.setOnEditorActionListener(onEditorActionListener);

protected OnEditorActionListener onEditorActionListener = new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            resetTimeout();
            InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            v.clearFocus();
        }

        return false;
    }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-14
    • 2011-03-11
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多