好的,你們都知道要隱藏你須要實現的鍵盤: android
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
但這裏最重要的是當用戶觸摸或選擇不是EditText
或softKeyboard的任何其餘地方時如何隱藏鍵盤? ide
我試圖在個人父Activity
上使用onTouchEvent()
,但只有當用戶觸摸任何其餘視圖以外而且沒有scrollview時才能使用。 工具
我嘗試實現觸摸,單擊,集中監聽器而沒有任何成功。 佈局
我甚至嘗試實現本身的scrollview來攔截觸摸事件,但我只能獲取事件的座標而不是點擊的視圖。 性能
有沒有一種標準的方法來作到這一點? 在iPhone中它真的很容易。 字體
有一種更簡單的方法,基於iPhone一樣的問題。 只需覆蓋觸摸事件的背景佈局,其中包含編輯文本。 只需在活動的OnCreate中使用此代碼(login_fondo是根佈局): ui
final LinearLayout llLogin = (LinearLayout)findViewById(R.id.login_fondo); llLogin.setOnTouchListener( new OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent ev) { InputMethodManager imm = (InputMethodManager) mActivity.getSystemService( android.content.Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mActivity.getCurrentFocus().getWindowToken(), 0); return false; } });
如下代碼段只是隱藏了鍵盤: this
public static void hideSoftKeyboard(Activity activity) { InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService( Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow( activity.getCurrentFocus().getWindowToken(), 0); }
您能夠將其放在實用程序類中,或者若是要在活動中定義它,請避免使用activity參數,或者調用hideSoftKeyboard(this)
。 spa
最棘手的部分是什麼時候調用它。 您能夠編寫一個迭代活動中每一個View
的方法,並檢查它是不是instanceof EditText
一個instanceof EditText
若是它沒有將setOnTouchListener
註冊到該組件,那麼一切都將落實到位。 若是你想知道如何作到這一點,事實上它很簡單。 這是你作的,你寫一個像下面這樣的遞歸方法,實際上你能夠用它來作任何事情,好比設置自定義字體等......這是方法 code
public void setupUI(View view) { // Set up touch listener for non-text box views to hide keyboard. if (!(view instanceof EditText)) { view.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { hideSoftKeyboard(MyActivity.this); return false; } }); } //If a layout container, iterate over children and seed recursion. if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { View innerView = ((ViewGroup) view).getChildAt(i); setupUI(innerView); } } }
這就是所有,只需在您的活動中設置setContentView
後調用此方法便可。 若是您想知道要傳遞什麼參數,它是父容器的id
。 將id
分配給父容器,如
<RelativeLayoutPanel android:id="@+id/parent"> ... </RelativeLayout>
並調用setupUI(findViewById(R.id.parent))
,就是這樣。
若是要有效地使用它,能夠建立一個擴展的Activity
並將此方法放入其中,並使應用程序中的全部其餘活動擴展此活動,並在onCreate()
方法中調用其setupUI()
。
但願能幫助到你。
若是您使用多於1個活動,則爲父佈局定義公共ID,如<RelativeLayout android:id="@+id/main_parent"> ... </RelativeLayout>
而後,從擴展類Activity
,並定義setupUI(findViewById(R.id.main_parent))
在其OnResume()
和擴大這一類,而不是``活動in your program
顯示/隱藏軟鍵盤的方法
InputMethodManager inputMethodManager = (InputMethodManager) currentActivity.getSystemService(Context.INPUT_METHOD_SERVICE); if (isShow) { if (currentActivity.getCurrentFocus() == null) { inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); } else { inputMethodManager.showSoftInput(currentActivity.getCurrentFocus(), InputMethodManager.SHOW_FORCED); } } else { if (currentActivity.getCurrentFocus() == null) { inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0); } else { inputMethodManager.hideSoftInputFromInputMethod(currentActivity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } }
我但願他們有用
我修改了Andre Luis IM的解決方案我實現了這個:
我建立了一個實用工具方法來隱藏軟鍵盤,就像Andre Luiz IM所作的那樣:
public static void hideSoftKeyboard(Activity activity) { InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); }
可是,不是爲每一個視圖註冊一個OnTouchListener,而是性能不好,我只爲root視圖註冊了OnTouchListener。 因爲事件一直消耗直到被消耗(EditText是默認使用它的視圖之一),若是它到達根視圖,那是由於它沒有被消耗,因此我關閉了軟鍵盤。
findViewById(android.R.id.content).setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Utils.hideSoftKeyboard(activity); return false; } });
我喜歡打電話的方法dispatchTouchEvent
經過htafoya作,可是:
因此,我作了一些更簡單的解決方案:
@Override public boolean dispatchTouchEvent(final MotionEvent ev) { // all touch events close the keyboard before they are processed except EditText instances. // if focus is an EditText we need to check, if the touchevent was inside the focus editTexts final View currentFocus = getCurrentFocus(); if (!(currentFocus instanceof EditText) || !isTouchInsideView(ev, currentFocus)) { ((InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE)) .hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } return super.dispatchTouchEvent(ev); } /** * determine if the given motionevent is inside the given view. * * @param ev * the given view * @param currentFocus * the motion event. * @return if the given motionevent is inside the given view */ private boolean isTouchInsideView(final MotionEvent ev, final View currentFocus) { final int[] loc = new int[2]; currentFocus.getLocationOnScreen(loc); return ev.getRawX() > loc[0] && ev.getRawY() > loc[1] && ev.getRawX() < (loc[0] + currentFocus.getWidth()) && ev.getRawY() < (loc[1] + currentFocus.getHeight()); }
有一個缺點:
從一個EditText
切換到另外一個EditText
使鍵盤隱藏和從新顯示 - 在個人狀況下,它須要這樣,由於它顯示您在兩個輸入組件之間切換。