/**
     * @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 解决输入法键盘遮盖布局问题的更多相关文章

  1. Android中如何解决输入法键盘和activity页面遮挡的问题

    不希望遮挡设置activity属性android:windowSoftInputMode="adjustPan" 希望动态调整高度android:windowSoftInputMo ...

  2. Android隐藏输入法键盘(hideSoftInputFromInputMethod没有效果)

    在个别时候,需要强制隐藏Android输入法键盘,如当前键盘正在显示,这个时候点击了侧滑面板,就要强制隐藏输入法键盘.网上常见的方法有: 1. InputMethodManager imm = (In ...

  3. android webview 输入法键盘遮挡输入框的问题

    新建一个工具类: /** * 解决webView键盘遮挡问题的类 * Created by zqy on 2016/11/14. */ public class KeyBoardListener { ...

  4. Android解决软键盘弹出将布局顶到上面

    有时候我们在下面的布局是一个RadioGroup,然后当页面中的EditText获得焦点的时候,会将地步的RadioGroup顶起来,这时候我们只需要在AndroidMainfest中RadioGro ...

  5. Android 关于显示键盘,布局错乱网上顶的问题

    <activity android:name="com.taiyi.DiscussActivity" android:windowSoftInputMode="st ...

  6. 手机浏览器浏览WebApp弹出的键盘遮盖住文本框的解决办法

    手机浏览器浏览WebApp弹出的键盘遮盖住文本框的解决办法 最近碰到Android微信内置浏览H5页面,因为其中的文本输入框(input)放置在靠近页面的中下方,点击文本框以后,则输入框会被弹出的手机 ...

  7. Android 输入法键盘和activity页面遮挡问题解决

    本文主要介绍Android中如何解决输入法键盘和activity页面遮挡的问题. 总结: 不希望遮挡设置activity属性android:windowSoftInputMode="adju ...

  8. Android中软键盘弹出时关于布局的问题

     当在Android的layout设计里面如果输入框过多,则在输入弹出软键盘的时候,下面的输入框会有一部分被软件盘挡住,从而不能获取焦点输入. 解决办法: 方法一:在你的activity中的oncre ...

  9. Android中软键盘弹出时底部菜单上移问题

    当在Android的layout设计里面如果输入框过多,则在输入弹出软键盘的时候,下面的输入框会有一部分被软件盘挡住,从而不能获取焦点输入. 解决办法: 方法一:在你的activity中的oncrea ...

随机推荐

  1. javascript基础拾遗(四)

    1.什么是闭包 正常函数,执行完毕后相关的参数,变量就释放掉了. 当一个函数的返回值是另一个函数时,该函数的相关参数和变量都会保存在返回的函数中,这种结构叫做闭包. 2.示例 计算数组和 functi ...

  2. 每日英语:Auto Makers Accelerate Efforts to Develop Self-Driving Cars

    Big auto makers are steering their efforts to develop cars that drive themselves out of the labs and ...

  3. vjue 点击发送邮件如何处理

    <li @click.stop="openSendMail()"> <i class="icons mail-icon" title=&quo ...

  4. Path-to-PegExp的使用

    下载: npm install path-to-regexp --save 引入: var pathToRegexp = require('path-to-regexp') 或者 import pat ...

  5. jackson中自定义处理序列化和反序列化

    http://jackyrong.iteye.com/blog/2005323 ********************************************** 对于一直用gson的人来说 ...

  6. Linux考前突击

    ### 试题 --- http://blog.51cto.com/12612579/2056140 ### 涉及到的知识点 --- Linux之通配符https://www.cnblogs.com/0 ...

  7. YII登陆错误 mcrypt_create_iv()

    使用php框架YII进行开发的时候,虽然很快,性能很好,但是也有一些小问题,现在记录下来. 1.在使用gii的自动代码生成后,登陆系统,就会报错 mcrypt_create_iv() [<a h ...

  8. Oracle 报错:PLS-00201: 必须声明标识符

    原因:调用其他用户的包或存储过程. 解决方法:在被调用的包或存储过程的用户下授权执行权限给调用用户: grant execute on 包名 to 用户名;

  9. spark 数据倾斜的一些表现

    spark 数据倾斜的一些表现 https://yq.aliyun.com/articles/62541

  10. mac OS X中升级php5.5至php5.6 or php7

    在做php项目中,提示“Warning. PHP 5.6 or newer is required. Please, upgrade your local PHP installation.” 通过p ...