【问题标题】:Keyboard overlapping EditText on click单击时键盘重叠 EditText
【发布时间】:2015-10-13 18:26:44
【问题描述】:

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

【问题讨论】:

  • 我认为您需要添加滚动视图作为布局的父级。之后您的代码可以工作。 this链接帮助你
  • 你试过在清单中设置吗?
  • @zgc7009 如何使用片段做到这一点?
  • 您可能可以在添加片段的活动上设置它。
  • @EdsonMenegatti 好的,我刚刚这样做了,但是从我第二次点击 editText 开始,键盘就重叠了

标签: android android-fragments android-edittext android-keypad


【解决方案1】:

如何设置RESIZE 而不是PAN?您将能够将您的EditText 保持在SoftKeyboard 之上。尝试如下:

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

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

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

然后,如您所见,EditText 位于软键盘上方,但从您的布局来看,您有一个带有android:layout_alignParentBottom="true" 属性的TextView,它将与EditText 重叠:

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

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

你会得到以下结果:

【讨论】:

  • @Filo 为什么通过调用 getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) 不能在 BottomSheetFragment 中运行相同的代码; i> 在 Oncreateview 中
【解决方案2】:

试试这个,将此代码添加到您的 AndroidManifest.xml 活动中

android:windowSoftInputMode="adjustPan" 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    相关资源
    最近更新 更多