软键盘覆盖密码EditText字段

时间:2014-04-10 16:53:04

标签: android android-layout

这个问题可能类似于thisthis,但它们并没有解决我的问题。我有一个登录UI,如下所示:

enter image description here

当我点击用户名字段时,它会变成这样:

enter image description here

正如您在此处所见,软键盘覆盖了密码字段,输入用户名后,您必须先关闭软键盘,然后单击密码字段以输入密码,这不是用户友好的。如何避免软键盘覆盖密码字段,以便用户可以直接输入密码。以下是此UI的布局。

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context="eda397.group10.navigator.MainActivity$PlaceholderFragment" >

<ImageView
    android:id="@+id/logo"
    android:layout_width="125dp"
    android:layout_height="125dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginTop="24dp"
    android:src="@drawable/ic_launcher" />


<Button
    android:id="@+id/register_button"
    android:layout_width="85dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/login_username"
    android:layout_marginBottom="65dp"
    style="@style/CustomStyleButton" 
    android:text="@string/button_register" />

<EditText
    android:id="@+id/login_username"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:layout_alignLeft="@+id/login_password"
    android:layout_alignRight="@+id/login_password"
    android:layout_centerVertical="true"
    android:background="#d8d8d8"
    android:ems="10"
    android:hint="@string/username_message" >

</EditText>

<EditText
    android:id="@+id/login_password"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:layout_below="@+id/login_username"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="17dp"
    android:background="#d8d8d8"
    android:ems="10"
    android:hint="@string/password_message"
    android:inputType="textPassword" />

<Button
    android:id="@+id/login_button"
    android:layout_width="85dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/register_button"
    android:layout_alignBottom="@+id/register_button"
    android:layout_alignLeft="@+id/login_username"
    style="@style/CustomStyleButton" 
    android:text="@string/button_send" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

在您的AndroidManifest.xml中,在您的活动声明中,您需要设置windowSoftInputMode,如下所示:

<activity
    android:name="..."
    android:windowSoftInputMode="adjustResize">
</activity>

此解决方案可能无法正常使用相对布局。您可以考虑更改为线性布局并使用windowSoftInputMode,或者您可以尝试更改布局重力:

<RelativeLayout
    ...
    android:layout_gravity="bottom" >

答案 1 :(得分:0)

如果您只是想让用户轻松从“用户名”到“密码”字段,您可以将imeOptions添加到login_username:

<EditText
    android:id="@+id/login_username"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:layout_alignLeft="@+id/login_password"
    android:layout_alignRight="@+id/login_password"
    android:layout_centerVertical="true"
    android:background="#d8d8d8"
    android:ems="10"
    android:hint="@string/username_message"
    android:imeOptions="actionNext" >

这不会改变屏幕上的键盘,但是如果你想解决这个问题,那么你在问题中提到的答案应该已经足够了。

相关问题