EditText的光标停留在提示文本之后

10

我的应用中有一个简单的登录activity,包含邮箱和密码字段。在邮件EditText中存在奇怪的问题,与hintcursor位置有关:

如图所示,默认情况下,cursor不在第一个位置。它显示在提示后面(好像hint是输入文本一样)。

layout而言,密码EditText与邮件相同,但没有这个问题:

有人知道为什么会出现这种情况吗?我该如何解决?


以下是活动的布局:

<ScrollView
    android:id="@+id/login_form"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        style="@style/LoginFormContainer"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:hint="@string/prompt_email"
            android:inputType="textEmailAddress"
            android:maxLines="1"
            android:singleLine="true" />

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:hint="@string/prompt_password"
            android:imeActionId="@+id/login"
            android:imeActionLabel="@string/action_sign_in_short"
            android:imeOptions="actionUnspecified"
            android:inputType="textPassword"
            android:maxLines="1"
            android:singleLine="true" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dip"
            android:gravity="center" >

            <!-- sign in button -->

            <TextView
                android:gravity="center"
                android:id="@+id/sign_in_button"
                android:layout_width="120dip"
                android:layout_height="50dip"
                android:layout_alignParentRight="true"
                android:layout_marginLeft="25dip"
                android:layout_marginRight="20dip"
                android:background="@drawable/btn_background_login_and_offered_and_followers"
                android:clickable="true"
                android:onClick="signInButton"
                android:text="@string/action_sign_in_register" />

            <!-- sign up button -->

            <TextView
                android:gravity="center"
                android:id="@+id/sign_up_button"
                android:layout_width="120dip"
                android:layout_height="50dip"
                android:layout_marginLeft="20dip"
                android:layout_toLeftOf="@id/sign_in_button"
                android:background="@color/Gray"
                android:onClick="signUpButton"
                android:text="@string/action_sign_up_register" />
        </RelativeLayout>
    </LinearLayout>
</ScrollView>
如果需要其他代码部分,请告诉我添加。

这很奇怪。您能否在不同环境中复现问题,例如它在设备和模拟器上同时发生?另外,您正在测试哪个版本的Android?我不是语言专家,但那个脚本看起来有点像阿拉伯语。如果是这样,请尝试使用 android:gravity="start" 而不是 android: gravity="left",看看 Android 的 RTL 支持是否引起了问题。这可能需要在清单文件中的 <application> 中添加 android:supportsRtl="true":http://android-developers.blogspot.fr/2013/03/native-rtl-support-in-android-42.html - CommonsWare
我正在使用安卓4.4的Nexus 4进行测试(我没有设置模拟器进行测试)。实际上,这些文本是波斯语。我的清单中有android:supportsRtl="true"。将其更改为android:gravity="start"会使光标向右移动并位于提示的开头。但我希望它在左边。 - Mahm00d
"这些文本实际上是波斯语" - 对于我猜错了脚本,我深表歉意。 "但我希望它在左边" - 您可以尝试使用 android:gravity="end" 然后看看会发生什么。 - CommonsWare
不幸的是,将重力更改为“end”并没有帮助。光标仍然与之前相同的位置。令我困惑的是EditText之间的差异...另外,我在安卓2.3的Galaxy Mini上进行了检查,它没有这个问题。 - Mahm00d
我在一台安装了Android 2.3的Galaxy Mini上进行了检查,它没有出现这个问题,因为2.3版本对RTL的支持有限。你正在使用的东西是在4.2及以上版本中添加的。而且这些东西需要让你的波斯语从右边开始。对我来说,这感觉像是一个框架错误,但我对RTL支持不够强大,无法确定。 - CommonsWare
显示剩余5条评论
6个回答

8

尝试使用以下代码:

android:textCursorDrawable="@null"

对我来说可行。


0
据我所理解,您想要为EditText添加波斯语提示(应该是RTL),并且输入文本是LTR(例如电子邮件、密码等)。
在我的应用程序中,我会这样做,在XML中:
<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="101dp"
    android:ems="10"
    android:hint="ایمیل را وارد نمائید" >

    <requestFocus />
</EditText>

而在Java中:

final EditText  editTxt = (EditText) findViewById(R.id.editText2);

        editTxt.setGravity(Gravity.RIGHT);
        editTxt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                if (editTxt.getText().length() == 0)
                {
                    editTxt.setGravity(Gravity.RIGHT);
                }
                else {
                    editTxt.setGravity(Gravity.LEFT);
                }

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

它给了我我想要的结果。提示对齐在右侧,当用户开始输入时,输入的文本将对齐到左侧,这样光标问题也会得到解决:D


谢谢您的回答。但是您误解了我的意思。我希望提示和文本都是左对齐的(EditText没有问题地左对齐)。问题是,当波斯语提示写在左对齐的EditText中时,光标停留在文本后面,而不是提示的开头。 - Mahm00d
啊哈,那么你可以看到,当你将inputType属性设置为True时,一切都会像你想要的那样美妙!然后去检查EditText源代码,它可能对你有用(我会尽快做到的:D) - MAY3AM
你说将 inputType 设置为 True 是什么意思?它接收的值像 "text"、"number" 等,而不是布尔值。正如你在我的代码中看到的那样,它被设置为 "textEmailAddress"。 - Mahm00d
抱歉我的表达不太好,我的意思是您需要将此属性设置为像textPassword这样的值。 - MAY3AM

0

试试这个:

android:gravity="left|center"

0
五年过去了,我们仍然存在这个问题。我想到的使光标看起来稍微好看一点的方法是对所有EditText使用android:textDirection="ltr"android:textDirection="rtl"。这将强制光标移动到提示的末尾/开头,当然我们仍然可以使用gravity将提示放在中心或任何我们想要的位置。

0

1
添加setSelection(0)没有解决问题。实际上,EditText长度为零,但光标不在开头。 - Mahm00d

0

您可以使用TextInputEditText,并使用android:textAlignment="viewStart"

这里是一个示例:

<com.google.android.material.textfield.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/enter_password"
                    android:inputType="textPassword"
                    android:paddingHorizontal="25dp"
                    android:paddingVertical="10dp"
                    android:textAlignment="viewStart"/>
            </com.google.android.material.textfield.TextInputLayout>

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