简体   繁体   English

键盘弹出时如何隐藏元素?

[英]How can i hide the elements when my keyboard pops up?

I have seen a few similar questions, but they want the things to show up, I want to hide them. 我已经看到了一些类似的问题,但是他们希望事物显示出来,我想隐藏它们。 Basically I have a bunch of elements in my RelativeLayout and when the activity shows, the keyboard pops up, since I have an editText, and the elements that are aligned in the Bottom go up along the keyboard, I don't want that, I want them to stay hidden, any idea how can I achieve this? 基本上,我的RelativeLayout中有一堆元素,当活动显示时,由于我有一个editText,并且弹出了键盘,并且在Bottom中对齐的元素沿着键盘向上,所以我不想这么做,希望他们保持隐藏状态,我知道该如何实现? This is the xml of my Activity: 这是我的Activity的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="@drawable/bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:id="@+id/invoices_layout"
    tools:context=".NavManager">

    <RelativeLayout
        android:id="@+id/mcr_bar_progress"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:visibility="visible">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:layout_marginTop="15dp"
            android:text="Agregar Productos"
            android:textColor="@android:color/white"
            android:textSize="34sp" />

        <TextView
            android:id="@+id/textGrid2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="0dp"
            android:text="Grupo MkTech Solutions"
            android:textColor="@android:color/white"
            android:textSize="15sp" />

        <EditText
            android:id="@+id/txtproducto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_marginStart="0dp"
            android:layout_marginTop="92dp"
            android:layout_marginEnd="0dp"
            android:hint="Ej: Refresco Natural"
            android:inputType="text"
            android:textColor="@color/white"
            android:textSize="15sp"
            app:backgroundTint="@color/white" />

        <ImageButton
            android:id="@+id/btnbuscar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/txtproducto"
            android:layout_alignEnd="@+id/txtproducto"
            android:layout_marginTop="4dp"
            android:layout_marginEnd="0dp"
            android:onClick="onClickSwitch"
            app:srcCompat="@drawable/research" />

        <TextView
            android:id="@+id/txttotal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:padding="10dp"
            android:textColor="@color/WhiteBgText"
            android:textSize="15sp" />

        <ImageButton
            android:id="@+id/btnlimpiarcarro"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:onClick="onClickCleanCart"
            android:padding="10dp"
            app:srcCompat="@drawable/quit" />

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="0dp"
            android:layout_marginTop="150dp"
            android:layout_marginBottom="50dp"
            android:fillViewport="true">

            <TableLayout
                android:id="@+id/tableInvoices"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="0dp"
                android:stretchColumns="*"></TableLayout>

        </ScrollView>

    </RelativeLayout>

</LinearLayout>

The last two elements before the scrollview are the ones that need to stay hidden. 滚动视图之前的最后两个元素是需要保持隐藏状态的元素。

add the following to your activity in the manifest: 将以下内容添加到清单中的活动中:

android:windowSoftInputMode="adjustPan"

from the API : API

The activity's main window is not resized to make room for the soft keyboard. 活动的主窗口未调整大小以为软键盘腾出空间。 Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. 而是,窗口的内容会自动平移,以使当前焦点不会被键盘遮挡,并且用户始终可以看到他们正在键入的内容。 This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window. 通常,这不如调整大小那么可取,因为用户可能需要关闭软键盘才能到达窗口的模糊部分并与之交互。

By my understanding of your question you do not want the keyboard to push the UI elements at the bottom of your layout. 根据我对您问题的理解,您不希望键盘将UI元素推到布局的底部。

To achieve this all you need to do is put the following line into your manifest activity tag: 为此,您需要做的就是将以下行放入清单活动标签中:

android:windowSoftInputMode="adjustNothing"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM