Android打开软键盘将按钮或布局顶在软键盘的上方


前言

在日常开发过程中经常会遇到提交保存等需求,例如在相对布局中上方有若干个输入框,提交保存按钮在布局的最下方,我们在输入完全部信息后,想要点击提交必须手动关闭软键盘,此时按钮是被遮挡状态,交互很不友好。


一、使用步骤

1.XML示例

代码如下(示例):

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:orientation="vertical"
    tools:ignore="MissingDefaultResource">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/ll_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/rl_top"
            android:background="@color/white"
            android:orientation="vertical"
            android:paddingLeft="@dimen/dp_15"
            android:paddingTop="@dimen/dp_10"
            android:paddingRight="@dimen/dp_15"
            android:paddingBottom="@dimen/dp_10">

            <TextView
                android:id="@+id/tv_one"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="一级支出分类名称"
                android:textColor="#333333"
                android:textSize="@dimen/dp_15"></TextView>

            <EditText
                android:id="@+id/et_one"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dp_5"
                android:hint="请输入一级支出分类名称"
                android:maxLength="5"
                android:maxLines="1"
                android:singleLine="false"
                android:textColor="#333333"
                android:textSize="@dimen/dp_18"
                android:theme="@style/MyEditText" />

        </LinearLayout>

        <Button
            android:id="@+id/btn_change"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_40"
            android:layout_alignParentBottom="true"
            android:layout_gravity="center"
            android:layout_margin="@dimen/dp_10"
            android:background="@drawable/btn_okl"
            android:backgroundTint="@null"
            android:text="保存"
            android:textColor="@color/white"
            android:textSize="16dp" />

    </RelativeLayout>
</ScrollView>

2.详解

代码如下(示例):

    android:fillViewport="true"
    tools:ignore="MissingDefaultResource"

通过查阅资料需要将这两个属性值加入到想要放在软键盘上方按钮的父布局中,尝试了相对布局、绝对布局等父控件都无法实现,所以这里使用ScrollView 布局并在里面加入这两个属性,在弹出软键盘的时候就可以把按钮顶在软件盘的上方


总结

以上就是今天要讲的内容,本文简单介绍了如何将在打开软键盘的时候将按钮顶在软键盘的上方。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

木易明~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值