天天看點

五種方法專治各種EditText和軟鍵盤的問題

很抱歉,公司上傳不了圖檔,下面的代碼直接拷貝到xml檔案和.java檔案裡即可運作看效果。

方法一:不需要設定Activity的輸入法模式,底部沒有懸浮的按鈕,核心思想是底部有一個view,平時是gone的,彈出軟鍵盤之後,變為visible,這樣輸入框就有足夠的空間上下滾動了。缺點:view的高度必須和軟鍵盤的高度一緻才完美,我這裡簡單設為100dp,其實是不完美的。推薦指數:1個星。

package com.sf.edittexttest;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

        final EditText et_test = (EditText) findViewById(R.id.et_test);
        final TextView tv_bottom = (TextView) findViewById(R.id.tv_bottom);
        et_test.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                et_test.getWindowVisibleDisplayFrame(r);
                if (et_test.getRootView().getHeight() - (r.bottom - r.top) > ) { // if more than 100 pixels, its probably a keyboard...
                    //鍵盤彈出了
                    tv_bottom.setVisibility(View.VISIBLE);
                } else {
                    //鍵盤隐藏了
                    tv_bottom.setVisibility(View.GONE);
                }
            }
        });
    }
}
           

xml檔案:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="500dp"
            android:background="@android:color/holo_blue_dark"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@android:color/holo_green_dark">

            <EditText
                android:id="@+id/et_test"
                android:layout_width="200dp"
                android:layout_height="60dp"
                android:background="@android:color/white"
                android:layout_centerInParent="true"/>
        </RelativeLayout>

        <TextView
            android:id="@+id/tv_bottom"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:visibility="gone"
            android:background="@android:color/holo_blue_dark"/>
    </LinearLayout>
</ScrollView>
           

方法二:不需要設定Activity的輸入法模式,不需要代碼,隻需要配置xml檔案,底部沒有常駐懸浮按鈕,自計算軟鍵盤高度,完美。推薦指數:5個星。

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="500dp"
                android:background="@android:color/holo_blue_dark" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="@android:color/holo_green_dark">

                <EditText
                    android:id="@+id/et_test"
                    android:layout_width="200dp"
                    android:layout_height="60dp"
                    android:layout_centerInParent="true"
                    android:background="@android:color/white" />
            </RelativeLayout>

            <TextView
                android:id="@+id/tv_bottom"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="@android:color/holo_blue_dark"
                android:visibility="gone" />
        </LinearLayout>
    </ScrollView>
</FrameLayout>
           

方法三:不需要設定Activity的輸入法模式,不需要代碼,但是常駐底部的按鈕會被頂上來。如果底部按鈕需要被頂上來的話,推薦指數:5個星,不需要頂上來的話,推薦指數:0個星。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="500dp"
                android:background="@android:color/holo_blue_dark" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="@android:color/holo_green_dark">

                <EditText
                    android:id="@+id/et_test"
                    android:layout_width="200dp"
                    android:layout_height="60dp"
                    android:layout_centerInParent="true"
                    android:background="@android:color/white" />
            </RelativeLayout>
        </LinearLayout>
    </ScrollView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我是常駐底部的按鈕,不會随ScrollView滾動,但會被軟鍵盤頂上來"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"/>
    </RelativeLayout>
</FrameLayout>
           

方法四:不需要配置Acitivity的輸入法模式,常駐底部的按鈕不會被頂上來。推薦指數:5個星。

代碼:

package com.sf.edittexttest;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

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

        final EditText et_test = (EditText) findViewById(R.id.et_test);
        final RelativeLayout rl_bottom = (RelativeLayout) findViewById(R.id.rl_bottom);
        final View view_bottom = findViewById(R.id.view_bottom);
        et_test.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                et_test.getWindowVisibleDisplayFrame(r);
                if (et_test.getRootView().getHeight() - (r.bottom - r.top) > ) { // if more than 100 pixels, its probably a keyboard...
                    //鍵盤彈出了
                    rl_bottom.setVisibility(View.GONE);
                    view_bottom.setVisibility(View.GONE);
                } else {
                    //鍵盤隐藏了
                    rl_bottom.setVisibility(View.VISIBLE);
                    view_bottom.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}
           

xml檔案:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="500dp"
                android:background="@android:color/holo_blue_dark" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="@android:color/holo_green_dark">

                <EditText
                    android:id="@+id/et_test"
                    android:layout_width="200dp"
                    android:layout_height="60dp"
                    android:layout_centerInParent="true"
                    android:background="@android:color/white" />
            </RelativeLayout>

            <!--我是底部的占位控件,我的高度必須和常駐底部的按鈕高度一緻,這樣在滾動到底部的時候,我上方的布局才能完整展示出來,軟鍵盤彈出之後我會隐藏,通常我是透明的-->
            <View
                android:id="@+id/view_bottom"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:background="@android:color/transparent" />
        </LinearLayout>
    </ScrollView>

    <!--這層布局占滿螢幕,但是背景為透明,是以能觸發ScrollView的滾動-->
    <RelativeLayout
        android:id="@+id/rl_bottom"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent">
        <!--這層布局高度和底部按鈕一樣,或稍大,作為按鈕的背景,也是常駐底部的,不随ScrollView而滾動-->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@android:color/holo_blue_dark"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:text="我是常駐底部的按鈕,不會随ScrollView滾動,軟鍵盤彈出之後我會隐藏,也就是說不會被軟鍵盤頂上來了" />
        </RelativeLayout>
    </RelativeLayout>
</FrameLayout>
           

方法五:activity配置android:windowSoftInputMode=”adjustPan”,代碼不再需要專門監聽軟鍵盤彈出來隐藏底部按鈕,配置該屬性之後,彈出軟鍵盤自動會覆寫底部常駐按鈕,隻是滾動不到底部了,ScrollView的一部分也被軟鍵盤遮住了,這時又回到方法一,必須設定底部view為軟鍵盤的高度才完美,我這裡沒有去計算軟鍵盤高度,推薦指數:1個星。

附上監聽軟鍵盤的方法,上面隻用到了其中的一種,朋友們根據需要自取。

監聽軟鍵盤方法一

監聽軟鍵盤方法二

監聽軟鍵盤方法三

如果覺得以上方法麻煩,不妨在Activity onCreate的時候,添上這句話試試:

//解決軟鍵盤彈出界面變型問題
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
           

這跟在清單檔案配置的效果是一樣的,結果都是不能完美把ScrollView的全部内容展示出來,其實和方法一差不多,都是要精确計算出軟鍵盤的高度才能完美。