短视频开发app,输入时点击空白区域收起输入法键盘

短视频开发app,输入时点击空白区域收起输入法键盘
1.java

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    
    
   if (event.getAction() == MotionEvent.ACTION_DOWN) {
    
    
       View v = getCurrentFocus();
       if ( v instanceof EditText) {
    
    
           Rect outRect = new Rect();
           v.getGlobalVisibleRect(outRect);
           if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
    
    
               v.clearFocus();
               InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
               imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
           }
       }
   }
   return super.dispatchTouchEvent( event );
}

2.kotlin

override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
    
    
        if (ev!!.getAction() == MotionEvent.ACTION_DOWN) {
    
    
            val v = currentFocus
            if (v is EditText) {
    
    
                val outRect = Rect()
                v.getGlobalVisibleRect(outRect)
                if (!outRect.contains(ev.getRawX().toInt(), ev.getRawY().toInt())) {
    
    
                    v.clearFocus()
                    val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0)
                }
            }
        }
        return super.dispatchTouchEvent(ev)
    }

以上就是 短视频开发app,输入时点击空白区域收起输入法键盘,更多内容欢迎关注之后的文章

猜你喜欢

转载自blog.csdn.net/yb1314111/article/details/125296173
今日推荐