单击时键盘重叠 EditText

时间:2023-05-04
本文介绍了单击时键盘重叠 EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在我的片段中,我在滚动视图中有一个 editText,当我点击它时,我将它设置为像这样打开:

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

从第一次打开它就可以正常工作,但是当我关闭键盘并再次打开它时,它与editText重叠,如何将editText设置为始终以SOFT_INPUT_ADJUST_PAN模式打开?

XML:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:custom="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/雪花石膏"><文本视图android:id="@+id/semconexao"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="验证 sua conexão com a internet"机器人:重力=中心"android:layout_marginTop="10dp"android:visibility="消失"/><滚动视图android:id="@+id/scroll"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_above="@+id/footer"><线性布局android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="垂直"><相对布局android:layout_width="fill_parent"android:layout_height="wrap_content"android:visibility="可见"><com.android.volley.toolbox.NetworkImageViewandroid:id="@+id/imageInst"android:layout_width="match_parent"android:layout_height="200dp"android:scaleType="centerCrop"安卓:可见性=可见"/><com.retornar.utils.CircledNetworkImageViewandroid:id="@+id/缩略图"android:layout_width="@dimen/logo_maior"android:layout_height="@dimen/logo_maior"android:layout_marginLeft="20dp"android:layout_marginTop="100dp"机器人:adjustViewBounds =真"android:scaleType="centerCrop"></com.retornar.utils.CircledNetworkImageView><图像视图android:id="@+id/更多"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_amp"机器人:可见性=消失"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:layout_alignParentEnd="true"/></相对布局><文本视图android:id="@+id/retornarTitulo"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_below="@+id/imageInst"android:layout_marginTop="20dp"机器人:重力=中心"安卓:填充=10dp"android:text="@string/retornarTitulo"android:textColor="@color/犰狳"android:textSize="@dimen/text2"/><文本视图android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_below="@+id/retornarTitulo"机器人:重力=中心"安卓:填充=10dp"android:text="@string/retornar"android:textColor="@color/犰狳"android:textSize="@dimen/text1"/><包括 layout="@layout/valores"/><编辑文本android:id="@+id/campovalor"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginBottom="60dp"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_marginTop="10dp"android:background="@drawable/shadowedittext"机器人:重力=中心"android:hint="ou entre com um valor"android:imeOptions="actionDone"android:inputType="numberDecimal"安卓:填充=10dp"android:singleLine="true"/><查看android:layout_width="match_parent"android:layout_height="1dp"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_marginTop="20dp"android:background="@color/gray"/><文本视图android:id="@+id/propText"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="30dp"android:layout_marginTop="20dp"机器人:重力=中心"安卓:填充=10dp"android:text="Propósito em destaque"android:textColor="@color/犰狳"android:textSize="@dimen/text2"安卓:可见性=消失"/><包括布局=@layout/inst_prop"/><图像视图android:id="@+id/maisprop"android:layout_width="wrap_content"android:layout_height="wrap_content"安卓:layout_gravity="center"android:layout_marginTop="20dp"android:src="@drawable/ic_proposito"安卓:可见性=消失"/><文本视图android:id="@+id/maisproptext"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="80dp"android:layout_marginTop="10dp"机器人:重力=中心"android:text="Mais propósitos"android:textColor="@color/犰狳"安卓:可见性=消失"/></线性布局></滚动视图><文本视图android:id="@+id/ret"fontPath="字体/Avenir-Bold.ttf"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"安卓:阿尔法=0.9"android:background="@color/blumine"机器人:重力=中心"安卓:填充=15dp"安卓:文本=DOAR"android:textColor="@color/white"/></相对布局>

解决方案

设置 RESIZE 而不是 PAN 怎么样?您将能够将 EditText 保持在 SoftKeyboard 之上.尝试如下:

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

但是,您可以在 manifest 中将其设置为 Activity (parent) 属性,而不是在运行时执行此操作.这也将在您的片段 (child) 中处理:

为了防止这种行为,只需将 ScrollView 设置在最后一个 TextView 上方,如下所示:

你会得到以下结果:

In my fragment I have an editText inside a scrollview and when I click on that I set it to open like this:

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

It works fine from the first time it opens, but when I close the keyboard and open it again, it overlaps the editText, how can I set the editText to always open in SOFT_INPUT_ADJUST_PAN mode?

the XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/alabaster">

    <TextView
        android:id="@+id/semconexao"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Verifique sua conexão com a internet"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:visibility="gone"/>


    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/footer">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:visibility="visible">



                <com.android.volley.toolbox.NetworkImageView
                    android:id="@+id/imageInst"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:scaleType="centerCrop"
                    android:visibility="visible" />

                <com.retornar.utils.CircledNetworkImageView
                    android:id="@+id/thumbnail"
                    android:layout_width="@dimen/logo_maior"
                    android:layout_height="@dimen/logo_maior"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="100dp"
                    android:adjustViewBounds="true"
                    android:scaleType="centerCrop">

                </com.retornar.utils.CircledNetworkImageView>

                <ImageView
                    android:id="@+id/more"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_amp"
                    android:visibility="gone"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true" />

            </RelativeLayout>


            <TextView
                android:id="@+id/retornarTitulo"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageInst"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:padding="10dp"
                android:text="@string/retornarTitulo"
                android:textColor="@color/armadillo"
                android:textSize="@dimen/text2" />


            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/retornarTitulo"
                android:gravity="center"
                android:padding="10dp"
                android:text="@string/retornar"
                android:textColor="@color/armadillo"
                android:textSize="@dimen/text1" />

            <include layout="@layout/valores" />


            <EditText
                android:id="@+id/campovalor"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="60dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/shadoweditext"
                android:gravity="center"
                android:hint="ou entre com um valor"
                android:imeOptions="actionDone"
                android:inputType="numberDecimal"
                android:padding="10dp"
                android:singleLine="true" />


            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="20dp"
                android:background="@color/gray" />

            <TextView
                android:id="@+id/propText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="30dp"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:padding="10dp"
                android:text="Propósito em destaque"
                android:textColor="@color/armadillo"
                android:textSize="@dimen/text2"
                android:visibility="gone" />

            <include layout="@layout/inst_prop" />

            <ImageView
                android:id="@+id/maisprop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="20dp"
                android:src="@drawable/ic_proposito"
                android:visibility="gone" />

            <TextView
                android:id="@+id/maisproptext"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="80dp"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:text="Mais propósitos"
                android:textColor="@color/armadillo"
                android:visibility="gone" />

        </LinearLayout>
    </ScrollView>

    <TextView
        android:id="@+id/ret"
        fontPath="fonts/Avenir-Bold.ttf"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:alpha="0.9"
        android:background="@color/blumine"
        android:gravity="center"
        android:padding="15dp"
        android:text="DOAR"
        android:textColor="@color/white" />

</RelativeLayout>

解决方案

What about setting RESIZE instead of PAN? You will be able to keep your EditText above the SoftKeyboard. Try as follows:

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

However, instead of doing this at runtime, you can set it in the manifest as an Activity (parent) attribute. This will also be handled in your fragment (child):

<activity
    android:name=".NameActivity"
    android:label="@string/app_name"
    android:windowSoftInputMode="stateAlwaysHidden|adjustResize">

Then, as you can see, the EditText is above the SoftKeyboard, but from your layout, you have a TextView with android:layout_alignParentBottom="true" attribute which will overlap the EditText:

To prevent this behaviour, just set the ScrollView above the last TextView as follows:

<ScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/ret"> <!-- instead of "footer" -->

And you will get the following result:

这篇关于单击时键盘重叠 EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!