Android解决全屏下WebView中输入框被键盘遮挡

来源:互联网 发布:淘宝千牛包邮怎么设置 编辑:程序博客网 时间:2024/05/16 16:02
  1. /**  
  2.  * 解决webView键盘遮挡问题的类  
  3.  * Created by zqy on 2016/11/14.  
  4.  */  
  5. public class KeyBoardListener {  
  6.     private Activity activity;  
  7. // private Handler mhanHandler;  
  8.   
  9.   
  10.     private View mChildOfContent;  
  11.     private int usableHeightPrevious;  
  12.     private FrameLayout.LayoutParams frameLayoutParams;  
  13.   
  14.     private static KeyBoardListener keyBoardListener;  
  15.   
  16.   
  17.     public static KeyBoardListener getInstance(Activity activity) {  
  18. // if(keyBoardListener==null){  
  19.         keyBoardListener=new KeyBoardListener(activity);  
  20. // }  
  21.         return keyBoardListener;  
  22.     }  
  23.   
  24.   
  25.     public KeyBoardListener(Activity activity) {  
  26.         super();  
  27. // TODO Auto-generated constructor stub  
  28.         this.activity = activity;  
  29. // this.mhanHandler = handler;  
  30.   
  31.     }  
  32.   
  33.   
  34.     public void init() {  
  35.   
  36.   
  37.         FrameLayout content = (FrameLayout) activity  
  38.                 .findViewById(android.R.id.content);  
  39.         mChildOfContent = content.getChildAt(0);  
  40.         mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(  
  41.                 new ViewTreeObserver.OnGlobalLayoutListener() {  
  42.                     public void onGlobalLayout() {  
  43.                         possiblyResizeChildOfContent();  
  44.                     }  
  45.                 });  
  46.         frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent  
  47.                 .getLayoutParams();  
  48.   
  49.   
  50.     }  
  51.   
  52.   
  53.     private void possiblyResizeChildOfContent() {  
  54.         int usableHeightNow = computeUsableHeight();  
  55.         if (usableHeightNow != usableHeightPrevious) {  
  56.             int usableHeightSansKeyboard = mChildOfContent.getRootView()  
  57.                     .getHeight();  
  58.             int heightDifference = usableHeightSansKeyboard - usableHeightNow;  
  59.             if (heightDifference > (usableHeightSansKeyboard / 4)) {  
  60. // keyboard probably just became visible  
  61.                 frameLayoutParams.height = usableHeightSansKeyboard  
  62.                         - heightDifference;  
  63.             } else {  
  64. // keyboard probably just became hidden  
  65.                 frameLayoutParams.height = usableHeightSansKeyboard;  
  66.             }  
  67.             mChildOfContent.requestLayout();  
  68.             usableHeightPrevious = usableHeightNow;  
  69.         }  
  70.     }  
  71.   
  72.   
  73.     private int computeUsableHeight() {  
  74.         Rect r = new Rect();  
  75.         mChildOfContent.getWindowVisibleDisplayFrame(r);  
  76.         return (r.bottom - r.top);  
  77.     }  
  78.  
  79.   

  80. 调用方式为:KeyBoardListener.getInstance(this).init();,即可解决全屏下,键盘遮挡问题。
原创粉丝点击