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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ithouse

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

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

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

打赏作者

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

抵扣说明:

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

余额充值