五种方法专治各种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) > 500) { // 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) > 500) { // 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的全部内容展示出来,其实和方法一差不多,都是要精确计算出软键盘的高度才能完美。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ithouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值