Android輸入法彈出,佈局上移,背景不會壓縮

非常感謝博主,剛好遇到了這個問題,完美解決 原址:[這裏寫鏈接內容](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

接下來就是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

同時,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

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

(function () { ('pre.prettyprint code').each(function () { var lines = (this).text().split(\n).length;var numbering = $('
    ').addClass('pre-numbering').hide(); (this).addClass(hasnumbering).parent().append( numbering); for (i = 1; i
    發表評論
    所有評論
    還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
    相關文章