實(shí)現(xiàn)功能:點(diǎn)擊EditText,軟鍵盤出現(xiàn)并且不會(huì)隱藏,點(diǎn)擊或者觸摸EditText以外的其他任何區(qū)域,軟鍵盤被隱藏;
1、重寫dispatchTouchEvent()方法,獲取當(dāng)前觸摸事件為DOWN的時(shí)候隱藏軟鍵盤
@Override public boolean dispatchTouchEvent(MotionEvent ev) { //Finger touch screen event if (ev.getAction() == MotionEvent.ACTION_DOWN) { // get current focus,Generally it is EditText View view = getCurrentFocus(); if (isShouldHideSoftKeyBoard(view, ev)) { hideSoftKeyBoard(view.getWindowToken()); } } return super.dispatchTouchEvent(ev); }2、isShouldHideInput()方法;
/** * Judge what situation hide the soft keyboard,click EditText view should show soft keyboard * @param v Incident event * @param event * @return */ private boolean isShouldHideSoftKeyBoard(View view, MotionEvent event) { if (view != null && (view instanceof EditText)) { int[] l = { 0, 0 }; view.getLocationInWindow(l); int left = l[0], top = l[1], bottom = top +view.getHeight(), right = left + view.getWidth(); if (event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) { // If click the EditText event ,ignore it return false; } else { return true; } } // if the focus is EditText,ignore it; return false; }3、hideSoftKeyBoard()方法;
/** * hide soft keyboard * @param token */ private void hideSoftKeyBoard(IBinder token) { if (token != null) { InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS); } }以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持武林網(wǎng)!
新聞熱點(diǎn)
疑難解答
圖片精選