您的位置:首页 > 移动开发 > Android开发

Android输入法弹出,布局上移,背景不会压缩

2017-03-16 16:05 766 查看
非常感谢博主,刚好遇到了这个问题,完美解决
原址:[这里写链接内容](http://blog.csdn.net/harryweasley/article/details/50266749)

转载请注明出处,谢谢:http://blog.csdn.net/harryweasley/article/details/50266749

Android的输入法弹出问题,一直是困扰很多开发人员的问题,当输入法弹出时,布局会被压缩,某些控件被遮挡住,但是需求可能并不想让该控件遮挡住,在做输入法的时候,你一定要知道这个属性,android:windowSoftInputMode,他有三个属性,分别是adjustUnspecified,adjustResize,adjustPan。我建议最好不要用默认的,即adjustUnspecified。关于更多这三个属性的知识,我之前的一个博客写的很详细,地址是:http://blog.csdn.net/harryweasley/article/details/49124385

如下图所示:



当输入法弹出来的时候,会遮盖住登录按钮,但是我现在并不想输入法遮盖住登录按钮,同时希望背景也不会被压缩。

现在的需求是,当输入法弹出后,登录以上的布局(包括登录)向上移动到输入法之上,并且,背景不会被压缩。

如下所示:



如果想让弹出的输入法,保证界面背景图不被压缩,那么一定要使用adjustPan属性。

看如下图所示的分析:



一个简单的模型,当输入法弹出的时候,可能登录按钮已经被遮盖住,那么我们只需要让登录按钮以上的布局通过scrollTo方式上移他被遮盖的距离就行了。

下面开始贴代码,代码很简单,先是布局文件:

<?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"
android:background="@drawable/update_empty">

<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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[/code]

接下来就是ManiActivity里的代码了,里面写了很详尽的注释,上面也解释了一些:

package com.example.myandrtest;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

private LinearLayout linearLayout;
private Button mSubmit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);

linearLayout = (LinearLayout) findViewById(R.id.root);
mSubmit = (Button) findViewById(R.id.submit);
controlKeyboardLayout(linearLayout, 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);
// 当前视图最外层的高度减去现在所看到的视图的最底部的y坐标
int rootInvisibleHeight = root.getRootView()
.getHeight() - rect.bottom;
Log.i("tag", "最外层的高度" + root.getRootView().getHeight());
// 若rootInvisibleHeight高度大于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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
[/code]

同时,Manifest里面的activity里,一定要是 android:windowSoftInputMode=”adjustPan”

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myandrtest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[/code]

本文章参考文章地址为:http://www.cnblogs.com/kobe8/p/4030412.html

(function () {('pre.prettyprint code').each(function () {
var lines = (this).text().split(′\n′).length;varnumbering = $('').addClass('pre-numbering').hide();
(this).addClass(′has−numbering′).parent().append(numbering);
for (i = 1; i
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息