如何在Android中单击侧面时隐藏虚拟键盘

0 android android-virtual-keyboard

我正在学习教程.

代码工作正常editText.但现在我有很多(近10个)EditText领域.如果我为每个字段重复此代码,代码将是冗长的.任何人都可以告诉我如何在任何字段外点击时禁用虚拟键盘?

Ram*_*ran 5

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
View view = getCurrentFocus();
boolean ret = super.dispatchTouchEvent(event);

if (view instanceof EditText) {
    View w = getCurrentFocus();
    int scrcoords[] = new int[2];
    w.getLocationOnScreen(scrcoords);
    float x = event.getRawX() + w.getLeft() - scrcoords[0];
    float y = event.getRawY() + w.getTop() - scrcoords[1];

    if (event.getAction() == MotionEvent.ACTION_UP 
&& (x < w.getLeft() || x >= w.getRight() 
|| y < w.getTop() || y > w.getBottom()) ) { 
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
    }
}
return ret;
}
Run Code Online (Sandbox Code Playgroud)

可能这会帮助你..检查一下