Bootstrap

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 布局并在里面加入这两个属性,在弹出软键盘的时候就可以把按钮顶在软件盘的上方


总结

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

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;