android 处理软键盘弹出时的一种方法

来源:互联网 发布:windows安全模式进不了 编辑:程序博客网 时间:2024/06/01 09:27

首先说明 我的思路

在查看了很多资料之后 发现了一些有用的 一些对我没用的
之后我的思路是这样的 :

先展示我的这里写图片描述
这里写图片描述

效果是 上部的内容不变 下面的ScrollView 整体上移动

思路:如果检测到软键盘弹出 就让scrollView高度缩小

额 思路看起来好简单
实现起来也是~~

我的软键盘模式

<activity android:name=".shouye.Shouye_Bianji"            android:windowSoftInputMode="adjustResize"/>

all.为页面的整体布局的linearlayout
main 是页面滚动的ScrollView

我的大致布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#333333"    android:focusable="true"    android:focusableInTouchMode="true"    android:clipToPadding="true"    android:id="@+id/all"    android:fitsSystemWindows="true">    <LinearLayout        android:id="@+id/top"        android:layout_width="fill_parent"        android:layout_height="43dp">    </LinearLayout>    <ScrollView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/main"        android:background="#f2f2f2">    </ScrollView>    <requestFocus/></LinearLayout>

得到main 在键盘没有的时候的高度

main = (ScrollView) findViewById(R.id.main);        main.post(new Runnable() {            @Override            public void run() {                main_height = main.getHeight();            }        });

这是得到 all 的高度

all = (LinearLayout) findViewById(R.id.all);        all.post(new Runnable() {            @Override            public void run() {                all_height = all.getBottom();                System.out.println("高度时--》" + all_height );            }        });

设置main 的高度

all.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {            @Override            public void onGlobalLayout() {                Rect rect = new Rect();                all.getWindowVisibleDisplayFrame(rect);                int mainInvisibleHeight = all_height - rect.bottom;//这是得到状态栏高度的一种方法                int result = 0;                int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");                if (resourceId > 0) {                    result = getResources().getDimensionPixelSize(resourceId);                }                if (mainInvisibleHeight > 100) {                    ViewGroup.LayoutParams layoutParams = main.getLayoutParams();                    layoutParams.height = mainInvisibleHeight - top_height + result * 2;                    System.out.println(result + " " + top_height + " " + all_height + " " + rect.bottom);                    main.setLayoutParams(layoutParams);                } else {                    ViewGroup.LayoutParams layoutParams = main.getLayoutParams();                    layoutParams.height = main_height;                    main.setLayoutParams(layoutParams);                }            }        });

如果你看到了下面,感谢,不过因为我一直认为思路比较重要,所以我一般不会写demo,而且这段时间比较乱,如果有时间,我一定坐下来写出直接可以调用的小框架,思路就是这样,当然,如果我忘了,也就忘了……

0 0
原创粉丝点击