点击时键盘遮挡EditText问题

19

在我的片段中,我有一个在滚动视图内的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/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>

我认为你需要将ScrollView添加为布局的父级,然后你的代码可能会起作用。这个链接可以帮助你。 - pRaNaY
你尝试过在清单文件中设置吗? - zgc7009
@zgc7009,我该如何使用片段来实现这个? - AND4011002849
您可能可以在添加该片段的活动上进行设置。 - Edson Menegatti
@EdsonMenegatti 好的,我刚刚做了那个,但是第二次点击editText时,键盘会覆盖它。 - AND4011002849
2个回答

32

试试将PAN改为RESIZE,这样您就可以将EditText保持在 SoftKeyboard上面了。操作如下:

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

不过,您可以将其作为Activity (父级)属性在manifest中设置,而不是在运行时执行此操作。这也将在您的碎片(子级)中处理:

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

如您所见,EditText 位于软键盘之上,但是从您的布局中可以看出,您有一个带有 android:layout_alignParentBottom="true" 属性的 TextView,这将重叠在 EditText 上:

1_screenshot_overlap_resize

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

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

您将获得以下结果:

2_screenshot_no_overlap_resize


3
@Filo 为什么在 BottomSheetFragment 中通过在 Oncreateview 中调用 getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); ,相同的代码不起作用? - LOG_TAG

23

尝试这个,在你的AndroidManifest.xml活动中添加这段代码

android:windowSoftInputMode="adjustPan" 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接