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

來源:互聯網
上載者:User

標籤:otto   美的   imm   location   ==   gety   ttext   動態   text   

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

 

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

 

[java] view plain copy 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/traceroute_rootview"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="@color/white"  
  6.     android:clickable="true"  
  7.     android:gravity="center_horizontal"  
  8.     android:orientation="vertical" >  
  9.   
  10. </LinearLayout>  

加上id和clickable=true

 

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

 

[java] view plain copy 
  1. findViewById(R.id.traceroute_rootview).setOnClickListener(this);  

在onClick中:

 

 

[java] view plain copy 
  1. @Override  
  2. public void onClick(View v) {  
  3.     switch (v.getId()) {  
  4.     case R.id.traceroute_rootview:  
  5.          InputMethodManager imm = (InputMethodManager)  
  6.          getSystemService(Context.INPUT_METHOD_SERVICE);  
  7.          imm.hideSoftInputFromWindow(v.getWindowToken(), 0);  
  8.         break;  
  9.     }  
  10.   
  11. }  


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

 

 

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

 

[java] view plain copy 
  1. @Override  
  2. public boolean dispatchTouchEvent(MotionEvent ev) {  
  3.     if (ev.getAction() == MotionEvent.ACTION_DOWN) {  
  4.         View v = getCurrentFocus();  
  5.         if (isShouldHideInput(v, ev)) {  
  6.   
  7.             InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
  8.             if (imm != null) {  
  9.                 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);  
  10.             }  
  11.         }  
  12.         return super.dispatchTouchEvent(ev);  
  13.     }  
  14.     // 必不可少,否則所有的組件都不會有TouchEvent了  
  15.     if (getWindow().superDispatchTouchEvent(ev)) {  
  16.         return true;  
  17.     }  
  18.     return onTouchEvent(ev);  
  19. }  


isShoudHideInput(View v,MotionEvent e)方法:

 

 

[java] view plain copy 
  1. public  boolean isShouldHideInput(View v, MotionEvent event) {  
  2.     if (v != null && (v instanceof EditText)) {  
  3.         int[] leftTop = { 0, 0 };  
  4.         //擷取輸入框當前的location位置  
  5.         v.getLocationInWindow(leftTop);  
  6.         int left = leftTop[0];  
  7.         int top = leftTop[1];  
  8.         int bottom = top + v.getHeight();  
  9.         int right = left + v.getWidth();  
  10.         if (event.getX() > left && event.getX() < right  
  11.                 && event.getY() > top && event.getY() < bottom) {  
  12.             // 點擊的是輸入框地區,保留點擊EditText的事件  
  13.             return false;  
  14.         } else {  
  15.             return true;  
  16.         }  
  17.     }  
  18.     return false;  
  19. }  

這種方法實現起來比較麻煩,解決思路與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.