Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

解決 Android EditText 被軟鍵盤遮擋的問題

編輯:Android 開發實例

寫登入介面的人大概都遇過這個狀況:點下輸入框後,虛擬鍵盤彈出來,下方程式裡的按鈕卻被擋住,使用者不得不先收起鍵盤才能按。很多人直覺的反應是去監聽鍵盤狀態,那其實是條又麻煩又不可靠的路;正確答案幾乎只是一行屬性設定。

核心答案:windowSoftInputMode

在 AndroidManifest.xml 的 Activity 上設定:

android:windowSoftInputMode="adjustResize"

效果是:軟鍵盤彈出時,系統會重新配置主視窗的版面,並觸發 onSizeChanged(),於是原本被遮住的按鈕自然被擠到鍵盤上方。

但有一個關鍵前提:介面不能設為全螢幕模式(例如套用 Theme.NoTitleBar.Fullscreen 或 FLAG_FULLSCREEN),否則設了 adjustResize 也不會有任何效果,因為系統根本不為你保留可縮小的空間。

相對地,設為 adjustPan 時,主視窗不會改變大小、不會呼叫 onSizeChanged(),系統只是把整個畫面往上推,讓焦點位置不被鍵盤蓋住。這種行為較不可控,通常不是我們想要的。

用實驗驗證差異

自訂一個 LinearLayout 子類別,把 onMeasure、onSizeChanged、onLayout 都攔下來打 log,就能看出系統實際做了什麼。

public class ResizeLayout extends LinearLayout {

    private static int count = 0;

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

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.e("onSizeChanged " + count++, "=>onSizeChanged called! w=" + w
                + ",h=" + h + ",oldw=" + oldw + ",oldh=" + oldh);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        Log.e("onLayout " + count++, "=>onLayout called! l=" + l
                + ", t=" + t + ", r=" + r + ", b=" + b);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.e("onMeasure " + count++, "=>onMeasure called! widthMeasureSpec="
                + widthMeasureSpec + ", heightMeasureSpec=" + heightMeasureSpec);
    }
}

配置檔把這個自訂 ViewGroup 當成根節點:

<com.winuxxan.inputMethodTest.ResizeLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/root_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/bottom_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="bottom">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello"
            android:background="#77777777" />
    </LinearLayout>

</com.winuxxan.inputMethodTest.ResizeLayout>

設為 adjustResize 後點擊文字框,log 顯示:

E/onMeasure 6: =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec=1073742024
E/onMeasure 7: =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec=1073742025
E/onSizeChanged 8: =>onSizeChanged called! w=320, h=201, oldw=320, oldh=377
E/onLayout 9: =>onLayout called! l=0, t=0, r=320, b=201

高度從 377 變成 201,onSizeChanged 確實被觸發——版面真的被壓縮了。

改成 adjustPan 後:

E/onMeasure 6: =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec=1073742200
E/onMeasure 7: =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec=1073742201
E/onLayout 8: =>onLayout called! l=0, t=0, r=320, b=377

雖然同樣重新 measure 與 layout,但沒有出現 onSizeChanged,代表版面尺寸完全沒變,只是內容被整體平移了。

其他可搭配的值

鍵盤的「狀態」與「行為」可以分開控制,寫在同一個屬性裡以 | 串接。

狀態相關:

  • stateUnspecified:不指定,由系統依主題決定(預設值)。
  • stateUnchanged:保持鍵盤上次的狀態。
  • stateHidden:使用者進入該 Activity 時隱藏鍵盤。
  • stateAlwaysHidden:該 Activity 視窗取得焦點時一律隱藏鍵盤。
  • stateVisible:在合適時機讓鍵盤保持可見。
  • stateAlwaysVisible:進入該 Activity 時一律讓鍵盤可見。

行為相關:

  • adjustUnspecified:不指定,由系統自動決定——通常會檢查視窗內是否有可捲動的版面,有的話就縮放視窗(預設值)。
  • adjustResize:一律縮放視窗以騰出鍵盤空間。
  • adjustPan:不縮放視窗,改為平移內容使焦點可見。

為什麼不建議包一層 ScrollView

常見的替代方案是在版面外再包一個 ScrollView:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/ll2"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <CheckBox android:id="@+id/widget57"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/location"
            android:layout_gravity="right">
        </CheckBox>

        <EditText android:id="@+id/widget34"
            android:layout_width="fill_parent"
            android:layout_height="400px"
            android:layout_weight="1"
            android:textColorHighlight="#cccccc"
            android:hint="你想說什麼?"
            android:gravity="top"
            android:textSize="18sp">
        </EditText>

        <Button android:id="@+id/widget53"
            android:layout_width="100px"
            android:layout_height="wrap_content"
            android:text="@string/share"
            android:layout_gravity="right"/>
    </LinearLayout>

</ScrollView>

鍵盤彈出時右側確實會出現捲軸,使用者可以往下捲再點按鈕。但體驗其實比直接收起鍵盤更差:多了一道手勢,而且使用者未必察覺有東西被遮住。

反觀 adjustResize 的效果:UI 與鍵盤互不遮擋,EditText 區域自動縮小,使用者既能持續輸入,也能直接按下按鈕,同時還看得到畫面全貌。這才是多數表單與登入頁該有的行為。

結論

不必改任何配置檔,也不必自己監聽鍵盤高度,只要在 AndroidManifest.xml 的 Activity 上加一行 android:windowSoftInputMode="adjustResize",並確認該 Activity 不是全螢幕模式,問題就解決了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved