android 监听软键盘的收起与打开

参考: http://toughcoder.net/blog/2015/10/09/android-trick-detect-soft-keyboard-show-slash-hide/

 1 package xiamai.testidea;
 2 
 3 import android.app.Activity;
 4 import android.graphics.Rect;
 5 import android.os.Bundle;
 6 import android.util.DisplayMetrics;
 7 import android.view.View;
 8 import android.view.ViewGroup;
 9 import android.view.ViewTreeObserver;
10 
11 /**
12  * Created by Administrator on 2016/6/15.
13  */
14 public class BaseActivity extends Activity
15 {
16     private OnKeyboardListener keyboardListener;
17     private boolean     keyboardVisible = false;
18     private ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener()
19     {
20         @Override
21         public void onGlobalLayout()
22         {
23             if (keyboardIsVisible(getRootView()))
24             {
25                 if (!keyboardVisible)
26                 {
27                     keyboardVisible = true;
28                     keyboardListener.onKeyboardShow();
29                 }
30             }
31             else
32             {
33                 if (keyboardVisible)
34                 {
35                     keyboardVisible = false;
36                     keyboardListener.onKeyboardHidden();
37                 }
38             }
39         }
40     };
41 
42     public View getRootView()
43     {
44         return ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);
45     }
46 
47     protected void setOnKeyboardListener(OnKeyboardListener keyboardListener)
48     {
49         this.keyboardListener = keyboardListener;
50         getRootView().getViewTreeObserver().addOnGlobalLayoutListener(globalLayoutListener);
51     }
52 
53     private boolean keyboardIsVisible(View rootView)
54     {
55         final int softKeyboardHeight = 100;
56         Rect r = new Rect();
57         rootView.getWindowVisibleDisplayFrame(r);
58         DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
59         int heightDiff = rootView.getBottom() - r.bottom;
60         return heightDiff > softKeyboardHeight * dm.density;
61     }
62 
63     public interface OnKeyboardListener
64     {
65         void onKeyboardShow();
66         void onKeyboardHidden();
67     }
68 }
View Code
 1 package xiamai.testidea;
 2 
 3 import android.os.Bundle;
 4 import android.util.Log;
 5 
 6 /**
 7  * Created by Administrator on 2016/7/11.
 8  */
 9 public class KeyboardTestActivity extends BaseActivity
10     implements BaseActivity.OnKeyboardListener
11 {
12     @Override
13     protected void onCreate(Bundle savedInstanceState)
14     {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.root_activity);
17         setOnKeyboardListener(this);
18     }
19 
20     @Override
21     public void onKeyboardShow()
22     {
23         Log.e("KeyboardEvent", "键盘打开");
24     }
25 
26     @Override
27     public void onKeyboardHidden()
28     {
29         Log.e("KeyboardEvent", "键盘关闭");
30     }
31 }
View Code

 

posted @ 2016-07-11 17:26  莫压枯枯地  阅读(596)  评论(0编辑  收藏  举报