Android 点击页面隐藏键盘

重写Activity页面的dispatchTouchEvent方法:

执行以下代码则可以触发点击除EditText输入框之外的地方隐藏键盘;

@Override
public boolean dispatchTouchEvent(MotionEvent ev)
{
    if (ev.getAction() == MotionEvent.ACTION_DOWN)
    {
        View v = getCurrentFocus();
        //如果搜索框内没有输入,隐藏键盘时则返回Contacts页面
        if (isShouldHideKeyboard(v, ev))
        {
            if (hideInputMethod(this, v))
            {
                logTrace.i("dispatchTouchEvent-->hideKeyboard"
                //这里可以执行隐藏需要执行的任务;
                //隐藏键盘时,其他控件不响应点击事件==》注释则不拦截点击事件
                return true;
            }
        }
    }
    return super.dispatchTouchEvent(ev);
}

public static Boolean hideInputMethod(Context context, View v)
{
    InputMethodManager imm =
            (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm != null)
    {
        return imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
    return false;
}

/**
 * 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘,因为当用户点击EditText时则不能隐藏
 *
 * @param v
 * @param event
 * @return
 */
private boolean isShouldHideKeyboard(View v, MotionEvent event)
{
    if (v != null && (v instanceof EditText))
    {
        int[] l = {0, 0};
        v.getLocationInWindow(l);
        int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left + v.getWidth();
        if (event.getX() > left && event.getX() < right && event.getY() > top &&
                event.getY() < bottom)
        {
            // 点击EditText的事件,忽略它。
            return false;
        }
        else
        {
            return true;
        }
    }
    // 如果焦点不是EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditText上,和用户用轨迹球选择其他的焦点
    return false;
}

点击Done或者隐藏键隐藏键盘:

  • contactsSearchEditText–>为点击弹出键盘的EditText;监听键值
    EditorInfo.IME_ACTION_NEXT和EditorInfo.IME_ACTION_DONE并执行隐藏键盘的代码则可以隐藏键盘;

    //键盘键值的监听
     contactsSearchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener()
     {
         @Override
         public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
         {
             Log.i("ContactFragment", "actionId:" + actionId);
             if (actionId == EditorInfo.IME_ACTION_NEXT ||
                     actionId == EditorInfo.IME_ACTION_DONE)
             {
                 // 隐藏软键盘
                 InputMethodManager imm = (InputMethodManager) mContext.getSystemService(
                         Context.INPUT_METHOD_SERVICE);
                 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                 backContactUI();
                 return true;
             }
             return false;
         }
     });
    

主动弹出键盘:

InputMethodManager imm =(InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(contactsSearchEditText, InputMethodManager.SHOW_IMPLICIT);

主动隐藏键盘:

InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(contactsSearchEditText.getWindowToken(), 0);

你可能感兴趣的:(Java基础)