Archive

Archive for October, 2014

Code สำหรับ Touch บนหน้าจอ หรือ Slide ลง แล้วปิด Keyboard

October 29, 2014 Leave a comment

ลองมาหลายตัวแล้ว อันนี้ได้ผลที่สุดครับ

@Override
public boolean dispatchTouchEvent(MotionEvent event) {

View v = getCurrentFocus();
boolean ret = super.dispatchTouchEvent(event);

if (v 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];

Log.d(“Activity”, “Touch event “+event.getRawX()+”,”+event.getRawY()+” “+x+”,”+y+” rect “+w.getLeft()+”,”+w.getTop()+”,”+w.getRight()+”,”+w.getBottom()+” coords “+scrcoords[0]+”,”+scrcoords[1]);
if (event.getAction() == MotionEvent.ACTION_UP && (x = w.getRight() || y w.getBottom()) ) {

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
}
}
return ret;
}

Categories: Android Tags: ,