美文网首页
android软键盘遮挡布局

android软键盘遮挡布局

作者: 涛涛123759 | 来源:发表于2017-02-28 14:28 被阅读145次

一、校正键盘
方法一:在你的activity中的oncreate中setContentView之前写上这个代码

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

方法二:
在项目的AndroidManifest.xml文件中界面对应的<activity>里加入
设置属性为 android:windowSoftInputMode="adjustResize" 即可自动调整高度。
设置属性为 android:windowSoftInputMode="adjustResize|stateHidden" 即可自动调整高度,且输入法不自动弹出。

二、自定义
1. MainActivity
OnGlobalLayoutListener 是ViewTreeObserver的内部类,当一个视图树的布局发生改变时,可以被ViewTreeObserver监听到,这是一个注册监听视图树的观察者(observer),在视图树的全局事件改变时得到通知。
总结为简单一句话:使用OnGlobalLayoutListener可以监听到布局的变化。
监听到布局变化后,我们就可以自由的操作了:
可以外包一个ScrollView来进行滚动,使用一个

scrollView.fullScroll(ScrollView.FOCUS_DOWN)

来滚动到底部。
不过,不使用ScrollView也能进行滚动的,例如LinearLayout也是可以滚动的,你还不知道吧,其实,我也是才知道:最基础的View就有个ScrollTo()函数的。
好了,基础知识准备好了,剩下就是滚动的距离计算了,我们这样来计算:
通过窗体的根View求出总的区域和可视区域,这样就可以计算出被遮挡的区域的高度,如果超过一定的值就判断为软键盘弹出了,然后将根View ScrollTo到一个位置,将被遮挡的View展示出来。
这里还有个要注意的地方,就是ScrollTo的参数,先看看函数原型:

public void scrollTo(int x, int y)

两个参数x、y,是要滚动到位置的坐标,注意,它们是绝对坐标。

public class MainActivity extends Activity {

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

}

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软键盘遮挡布局

      本文链接:https://www.haomeiwen.com/subject/uouxgttx.html