Android:软键盘将TextEdit移出屏幕

时间:2011-08-23 12:43:02

标签: android

我正在尝试使用软键盘,以便用户可以输入他的信息在我的Android应用程序上创建一个新帐户。问题是我的一些TextEdits总是在屏幕外流动,用户无法看到他实际键入的内容。屏幕看起来像这样:

screenshot

此屏幕使用以下xml代码构建:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">     

    <com.markupartist.android.widget.ActionBar 
       android:id="@+id/actionbar" 
       style="@style/ActionBar"/>      

    <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1.0">

    <LinearLayout
        android:layout_gravity="bottom"
        android:paddingTop="15dip"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"> 

       <EditText android:id="@+id/txtPhoneNumber" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dip"
            android:layout_marginRight="15dip"
            android:nextFocusUp="@+id/LoginButton"
            android:nextFocusDown="@+id/txtPassword" 
            android:hint="Phone Number"
            android:inputType="number" 
            android:singleLine="true" />

       <EditText android:id="@+id/txtPassword" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dip"
            android:layout_marginLeft="15dip"
            android:layout_marginRight="15dip"
            android:hint="Password"
            android:inputType="textPassword"
            android:singleLine="true"
            android:nextFocusDown="@+id/txtName"/>

       <EditText android:id="@+id/txtName" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dip"
            android:layout_marginLeft="15dip"
            android:layout_marginRight="15dip"
            android:hint="Name"
            android:singleLine="true"
            android:nextFocusDown="@+id/txtPin"  /> 

        <EditText android:id="@+id/txtPin" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dip"
            android:layout_marginLeft="15dip"
            android:layout_marginRight="15dip"
            android:hint="Pin"
            android:inputType="number"
            android:singleLine="true" />            

    </LinearLayout>

    </ScrollView>

    <LinearLayout android:id="@+id/buttonsLayout" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dip"
            android:layout_marginLeft="15dip"
            android:layout_marginRight="15dip" >    

    <Button android:id="@+id/btnLogin"
        android:layout_width="fill_parent"          
        android:layout_height="wrap_content" 
        android:layout_weight="1.0" 
        android:text="@string/btnLogin"                     
        android:onClick="onRegisterClick" />                            

</LinearLayout>   

在我的AndroidManifest.xml中,我以这种方式定义了我的Activity:

<activity android:name=".activity.NewAccountActivity" 
      android:label="@string/app_name" 
      android:screenOrientation="portrait" 
      android:windowSoftInputMode="stateHidden|adjustResize" />

我已经尝试了我的布局和windowSoftInputMode的不同组合,但我永远无法将正在更改的TextEdit显示在屏幕上。我在这里错过了什么吗?

感谢您的回复 Ť

3 个答案:

答案 0 :(得分:4)

在AndroidManifest.xml中,在活动代码中添加此行。

android:windowSoftInputMode="adjustPan" 
像这样

<activity android:name=".activity.NewAccountActivity" 
      android:label="@string/app_name" 
      android:screenOrientation="portrait" 
      android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"/>

希望它有效。

答案 1 :(得分:2)

你的scrollview设置为fill_parent,这不会导致控件垂直剪裁吗?如果要将其垂直拉伸,请将layout_height设置为0dip。

您可能还想尝试替换活动的windowSoftInputMode并将adjustResize替换为adjustPan

答案 2 :(得分:1)

您可以使用adjustPan将字段保留在屏幕上的可见区域内:

android:windowSoftInputMode="stateHidden|adjustPan"

<强>更新

请勿在{{1​​}}之外使用View。我已将您的布局更改为一个顶级ScrollView和内ScrollView。所有其他元素都位于LinearLayout内。这是因为Android在调整软键盘的屏幕时不知道如何正确滚动LinearLayout

ScrollView