天天看點

Android軟鍵盤遮擋的解決方案0問題概述1簡單解決

0問題概述

在編輯框輸入内容時會彈出軟鍵盤,而手機螢幕區域有限往往會遮住輸入界面,我們先看一下問題效果圖:

Android軟鍵盤遮擋的解決方案0問題概述1簡單解決

輸入使用者名和密碼時,系統會彈出鍵盤,造成系統鍵盤會擋住文本框的問題,如圖所示:

1簡單解決

1.1方法一

在你的activity中的oncreate中setContentView之前寫上這個代碼:

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

1.2方法二

在項目的AndroidManifest.xml檔案中界面對應的<activity>裡加入 。 這樣會讓螢幕整體上移。如果加上的 是 android:windowSoftInputMode="adjustPan"這樣鍵盤就會覆寫螢幕。那“android:windowSoftInputMode”是什麼呢?activity主視窗與軟鍵盤的互動模式,可以用來避免輸入法面闆遮擋問題,Android1.5後的一個新特性。這個屬性能影響兩件事情:

(1)當有焦點産生時,軟鍵盤是隐藏還是顯示

(2)是否減少活動主視窗大小以便騰出空間放軟鍵盤 它的設定必須是下面清單中的一個值,或一個”state…”值加一個”adjust…”值的組合。在任一組設定多個值——多個”state…”values,例如&mdash有未定義的結果。各個值之間用|分開。

android:windowSoftInputMode="stateVisible|adjustPan"
           

在這設定的值(除"stateUnspecified"和"adjustUnspecified"以外)将覆寫在主題中設定的值。Activity屬性“android:windowSoftInputMode”有不同的值,它們的差別如下所示: (1)stateUnspecified:軟鍵盤的狀态并沒有指定,系統将選擇一個合适的狀态或依賴于主題的設定

(2)stateUnchanged:當這個activity出現時,軟鍵盤将一直保持在上一個activity裡的狀态,無論是隐藏還是顯示

(3)stateHidden:使用者選擇activity時,軟鍵盤總是被隐藏

(4)stateAlwaysHidden:當該Activity主視窗擷取焦點時,軟鍵盤也總是被隐藏的

(5)stateVisible:軟鍵盤通常是可見的

(6)stateAlwaysVisible:使用者選擇activity時,軟鍵盤總是顯示的狀态

(7)adjustUnspecified:預設設定,通常由系統自行決定是隐藏還是顯示

(8)adjustResize:該Activity總是調整螢幕的大小以便留出軟鍵盤的空間

(9)adjustPan:目前視窗的内容将自動移動以便目前焦點從不被鍵盤覆寫和使用者能總是看到輸入内容的部分

1.3方法三

把頂級的layout替換成ScrollView,或者說在頂級的Layout上面再加一層ScrollView。這樣就會把軟鍵盤和輸入框一起滾動了,軟鍵盤會一直處于底部。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--解決方法三-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/img"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/login_bg" />

        <EditText
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/img"
            android:layout_marginBottom="10dip"
            android:layout_marginTop="10dip"
            android:hint="@string/name" />

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dip"
            android:hint="@string/password" />
    </LinearLayout>

</ScrollView>
           

前面三種方法雖然簡單,但是并不能達到預期的效果,方法四将使用代碼可控的方法解決這個問題。

1.4方法四

1.4.1布局檔案

以下為布局檔案:keyboard_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<com.dyx.skd.view.SoftKeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/layout_img"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:background="#ff0000"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/img"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="fitXY"
            android:src="@mipmap/login_bg" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        android:layout_weight="3"
        android:orientation="vertical">

        <EditText
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_alignParentBottom="true"
            android:layout_gravity="center_vertical"
            android:ems="10"
            android:hint="@string/name">

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_alignParentBottom="true"
            android:layout_gravity="center_vertical"
            android:ems="10"
            android:hint="@string/password">

            <requestFocus />
        </EditText>
    </LinearLayout>
</com.dyx.skd.view.SoftKeyboardView>
           

1.4.2自定義LinearLayout

下面為自定義LinearLayout:SoftKeyboardView.java

package com.dyx.skd.view;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.LinearLayout;


/**
 * Created by Richard.Da on 2015/7/16.
 */
public class SoftKeyboardView extends LinearLayout {
    public static final int KEYBOARD_HIDE = 0;
    public static final int KEYBOARD_SHOW = 1;
    public static final int KEYBOARD_MIN_HEIGHT = 60;

    private Handler mHandler = new Handler();

    public SoftKeyboardView(Context context) {
        super(context);
    }

    public SoftKeyboardView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, final int h, int oldw, final int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (oldh - h > KEYBOARD_MIN_HEIGHT) {
                    mKeyBoardStateChangeListener.stateChanged(KEYBOARD_SHOW);
                } else {
                    if (mKeyBoardStateChangeListener != null) {
                        mKeyBoardStateChangeListener.stateChanged(KEYBOARD_HIDE);
                    }
                }
            }
        });
    }

    private MyKeyBoardStateChangeListener mKeyBoardStateChangeListener;

    public void setMyKeyBoardStateChangeListener(MyKeyBoardStateChangeListener keyBoardStateChangeListener) {
        this.mKeyBoardStateChangeListener = keyBoardStateChangeListener;
    }

    //監聽軟鍵盤變化的接口
    public interface MyKeyBoardStateChangeListener {
        public void stateChanged(int stateCode);
    }
}
           

1.4.3主界面Activity

下面為界面Activity:MainActivity.java

package com.dyx.skd;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.dyx.skd.view.SoftKeyboardView;


public class MainActivity extends Activity implements SoftKeyboardView.MyKeyBoardStateChangeListener {
    private SoftKeyboardView rootLl;
    private LinearLayout imgLl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * 解決方法一
         */
        // getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.keyboard_layout);
        rootLl = (SoftKeyboardView) findViewById(R.id.layout_root);
        imgLl = (LinearLayout) findViewById(R.id.layout_img);
        rootLl.setMyKeyBoardStateChangeListener(this);
    }


    @Override
    public void stateChanged(int stateCode) {
        switch (stateCode) {
            case SoftKeyboardView.KEYBOARD_HIDE:
                imgLl.setVisibility(View.VISIBLE);
                break;
            case SoftKeyboardView.KEYBOARD_SHOW:
                imgLl.setVisibility(View.GONE);
                break;
        }
    }
}
           

1.4.4實作效果

鍵盤彈出:

Android軟鍵盤遮擋的解決方案0問題概述1簡單解決

鍵盤關閉:

Android軟鍵盤遮擋的解決方案0問題概述1簡單解決