zoukankan      html  css  js  c++  java
  • android 解决输入法键盘遮盖布局问题

        /**
         * @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

    public class MainActivity extends Activity {
        
        private LinearLayout mRoot;
        private Button mSubmit;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mRoot = (LinearLayout) findViewById(R.id.root);
            mSubmit = (Button) findViewById(R.id.submit);
            controlKeyboardLayout(mRoot, mSubmit);
        }
        
        /**
         * @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);
                    }
                }
            });
        }

    }

    2. activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/root"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="center_vertical" >
        
        <EditText android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:hint="edit1"/>
        <EditText android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:hint="edit2"/>
        <EditText android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:hint="edit3"/>
        <Button android:id="@+id/submit"
            android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:text="submit"/>

    </LinearLayout>

  • 相关阅读:
    Android文字上下滚动
    Java怎么去除字符串中的多个空白【split(" ")与split("\s+")】
    Java:Scanner.nextLine()和Scnner.next()的区别,以及多条nextLine()输入问题的解决方法
    SQL server报错18456(又名SQL server怎么用sa登入)
    Android Studio快速自动生成findViewById
    java.sql.Date和java.util.Date的联系与区别【转载】
    Linux Crontab 不执行
    PostgreSQL 全量 增量 归档 备份工具 pg_rman介绍与使用(转载)
    Rman备份的保留策略(retention policy) (转载)
    Oracle RMAN block_change_tracking(块更改追踪)
  • 原文地址:https://www.cnblogs.com/kobe8/p/4030412.html
Copyright © 2011-2022 走看看