繁体   English   中英

键盘弹出时调整EditTexts

[英]Adjusting EditTexts when keyboard pops up

我正在申请通讯录。 Update Contact活动负责更新现有联系人的详细信息。

下图是update contact活动的界面

在此处输入图片说明

当我单击任何文本字段时,Android设备键盘与文本字段重叠。 因此,为解决该问题,我添加了以下代码行以update activity清单文件中的update activity

android:windowSoftInputMode="adjustPan" 

添加以上行后,当单击名称字段时,我得到以下界面

在此处输入图片说明

问题

当前,仅在单击时或当我将光标从名称字段移动到电话字段时,电话字段才会显示在键盘上。

我希望电话字段也位于键盘上方,即使单击名称字段也是如此。 我怎样才能做到这一点?

下图是单击名称字段时我想要的

在此处输入图片说明

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

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

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

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

然后,如您所见, EditText在SoftKeyboard的上方,但是从您的布局来看,您有一个TextView具有android:layout_alignParentBottom="true"属性,该属性将与EditText重叠:

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

<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" > 

        <ScrollView android:id="@+id/scrollView1"
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent" 
        android:weightSum="1">
---
---
---
        </ScrollView> 
</LinearLayout> 

您将得到以下结果:

在此处输入图片说明

将您的两个字段放在LinearLayout 如下所示

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_gravity="bottom"
        android:id="@+id/ll">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="name"
            android:id="@+id/edit1"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="password"
            android:id="@+id/edit2"/>
    </LinearLayout>

在活动内部的Manifest.xml写入此标签。

android:windowSoftInputMode="adjustResize"

暂无
暂无

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

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