android 解决输入法键盘遮盖布局问题

转载自:http://blog.csdn.net/yqichang/article/details/11705235

这里采用滚动布局来解决输入法遮盖布局的问题,方法如下:

    /**
     * @param root 最外层布局,需要调整的布局
     * @param scrollToView 被键盘遮挡的scrollToView,滚动root,使scrollToView在root可视区域的底部
     */
    private void controlKeyboardLayout(final View root, final View scrollToView) {
        root.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                //获取root在窗体的可视区域
                root.getWindowVisibleDisplayFrame(rect);
                //获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)
                int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;
                //若不可视区域高度大于100,则键盘显示
                if (rootInvisibleHeight > 100) {
                    int[] location = new int[2];
                    //获取scrollToView在窗体的坐标
                    scrollToView.getLocationInWindow(location);
                    //计算root滚动高度,使scrollToView在可见区域的底部
                    int srollHeight = (location[1] + scrollToView.getHeight()) - rect.bottom;
                    root.scrollTo(0, srollHeight);
                } else {
                    //键盘隐藏
                    root.scrollTo(0, 0);
                }
            }
        });
    }

效果图如下:

下面提供完整的代码及布局文件:

1. MainActivity

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class MainActivity extends Activity {  
  2.       
  3.     private LinearLayout mRoot;  
  4.     private Button mSubmit;  
  5.       
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.         mRoot = (LinearLayout) findViewById(R.id.root);  
  11.         mSubmit = (Button) findViewById(R.id.submit);  
  12.         controlKeyboardLayout(mRoot, mSubmit);  
  13.     }  
  14.       
  15.     /** 
  16.      * @param root 最外层布局,需要调整的布局 
  17.      * @param scrollToView 被键盘遮挡的scrollToView,滚动root,使scrollToView在root可视区域的底部 
  18.      */  
  19.     private void controlKeyboardLayout(final View root, final View scrollToView) {  
  20.         root.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() {  
  21.             @Override  
  22.             public void onGlobalLayout() {  
  23.                 Rect rect = new Rect();  
  24.                 //获取root在窗体的可视区域  
  25.                 root.getWindowVisibleDisplayFrame(rect);  
  26.                 //获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)  
  27.                 int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;  
  28.                 //若不可视区域高度大于100,则键盘显示  
  29.                 if (rootInvisibleHeight > 100) {  
  30.                     int[] location = new int[2];  
  31.                     //获取scrollToView在窗体的坐标  
  32.                     scrollToView.getLocationInWindow(location);  
  33.                     //计算root滚动高度,使scrollToView在可见区域  
  34.                     int srollHeight = (location[1] + scrollToView.getHeight()) - rect.bottom;  
  35.                     root.scrollTo(0, srollHeight);  
  36.                 } else {  
  37.                     //键盘隐藏  
  38.                     root.scrollTo(00);  
  39.                 }  
  40.             }  
  41.         });  
  42.     }  
  43.   
  44. }  

2. activity_main.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/root"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical"  
  7.     android:gravity="center_vertical" >  
  8.       
  9.     <EditText android:layout_width="fill_parent"  
  10.         android:layout_height="50dip"  
  11.         android:hint="edit1"/>  
  12.     <EditText android:layout_width="fill_parent"  
  13.         android:layout_height="50dip"  
  14.         android:hint="edit2"/>  
  15.     <EditText android:layout_width="fill_parent"  
  16.         android:layout_height="50dip"  
  17.         android:hint="edit3"/>  
  18.     <Button android:id="@+id/submit"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="50dip"  
  21.         android:text="submit"/>  
  22.   
  23. </LinearLayout>  


经测试 这种办法可以解决输入法遮挡问题,还可以自己扩展不一定要滚动也可以 在弹出输入法时隐藏一些控件 达到比较好的效果
转自:http://blog.csdn.net/yqichang/article/details/11705235

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值