Android點擊EditText文字框之外任何地方隱藏鍵盤的解決辦法

來源:互聯網
上載者:User

標籤:

1,實現方法一:通過給當前介面布局檔案的父layout設定點擊事件(相當於給整個Activity設定點擊事件),在事件裡進行鍵盤隱藏

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:id="@+id/traceroute_rootview"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:background="@color/white"      android:clickable="true"      android:gravity="center_horizontal"      android:orientation="vertical" >    </LinearLayout> 

  加上id和clickable=true

 

然後在onCreate裡,添加onClick事件的監聽:

@Override  public void onClick(View v) {      switch (v.getId()) {      case R.id.traceroute_rootview:           InputMethodManager imm = (InputMethodManager)           getSystemService(Context.INPUT_METHOD_SERVICE);           imm.hideSoftInputFromWindow(v.getWindowToken(), 0);          break;      }    }  

  

這樣就可以完美的解決了輸入框外的隱藏效果,對於布局不是特別複雜或是其它觸摸事件少的情況下可以使用。

 

 

2,實現思路二:通過dispatchTouchEvent每次ACTION_DOWN事件中動態判斷非EditText本身地區的點擊事件,然後在事件中進行屏蔽。

@Override  public boolean dispatchTouchEvent(MotionEvent ev) {      if (ev.getAction() == MotionEvent.ACTION_DOWN) {          View v = getCurrentFocus();          if (isShouldHideInput(v, ev)) {                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);              if (imm != null) {                  imm.hideSoftInputFromWindow(v.getWindowToken(), 0);              }          }          return super.dispatchTouchEvent(ev);      }      // 必不可少,否則所有的組件都不會有TouchEvent了      if (getWindow().superDispatchTouchEvent(ev)) {          return true;      }      return onTouchEvent(ev);  }  

  

isShoudHideInput(View v,MotionEvent e)方法:

public  boolean isShouldHideInput(View v, MotionEvent event) {      if (v != null && (v instanceof EditText)) {          int[] leftTop = { 0, 0 };          //擷取輸入框當前的location位置          v.getLocationInWindow(leftTop);          int left = leftTop[0];          int top = leftTop[1];          int bottom = top + v.getHeight();          int right = left + v.getWidth();          if (event.getX() > left && event.getX() < right                  && event.getY() > top && event.getY() < bottom) {              // 點擊的是輸入框地區,保留點擊EditText的事件              return false;          } else {              return true;          }      }      return false;  }  

  這種方法實現起來比較麻煩,解決思路與iOS中的事件分發機制是類似,對於處理隱藏事件比較清晰,通過層層事件分發,然後判斷是否在需要屏蔽的地區。

 

 

Android點擊EditText文字框之外任何地方隱藏鍵盤的解決辦法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.