EditText提示中的光标在阿拉伯语下不从右侧开始

6

我正在尝试制作带有提示文本的EditText: 在英语中是“password”..光标正确地设置在左侧。 但对于阿拉伯语,提示为“كلمه المرور”,光标始终设置在左侧(提示的末尾)而非右侧。

<EditText
        android:id="@id/ETPass"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/etUsrName"
        android:layout_marginLeft="@dimen/_25sdp"
        android:layout_marginRight="@dimen/_25sdp"
        android:layout_marginTop="@dimen/_5sdp"
        android:background="@drawable/signup_edittext_input"
        android:ellipsize="start"
        android:gravity="center|right"
        android:hint="@string/Password"
        android:imeOptions="actionNext"
        android:inputType="textPassword"
        android:paddingRight="@dimen/_5sdp"
        android:singleLine="true"
        android:textColor="@color/orange"
        android:textColorHint="@color/orange" />

这种情况仅在 android:inputType="textPassword" 时发生。对于普通文本的 inputType,一切正常运作。

为什么要使用自定义编辑文本? - amorenew
在我的自定义类中,只需设置特定字体一次即可...这不是问题。 - user3623824
我使用 https://github.com/chrisjenx/Calligraphy 来支持任何视图或整个活动的自定义字体。 - amorenew
谢谢,我会检查它。 - user3623824
6个回答

3

Android 17及以上版本(4.2.+)可以正常工作:

android:textAlignment="viewStart"

1

尝试:

android:textDirection="rtl"

0

editText.setSelection(editText.getText().length());


0

XML代码

   <EditText
                android:maxLength="@integer/lockMaxLen"
                android:inputType="textPassword"
                android:layout_width="180dip"
                android:gravity="right|center"
                android:hint="@string/lock_pass_hint"
                android:layout_height="45dip"
                android:id="@+id/lockPassword" />

在onCreate中

 passwordEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence currentDigits, int start,
                                      int before, int count) {
                if (passwordEditText.getText().toString().length() == 0)
                    passwordEditText.setGravity(Gravity.RIGHT);
                else
                    passwordEditText.setGravity(Gravity.LEFT);
            }

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

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

0
Use both, to resolve the alignment issue (even in Samsung device also)

android:textAlignment="viewStart"
android:textDirection="rtl"

0
也许我有点晚了,但我遇到了同样的问题。我通过以下解决方法解决了这个问题。
// Get Current language

public static String getCurrentLanguage(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getString(CURRENT_LANGUAGE, "");
    }

// Check if the language is RTL

public static boolean isRTL(String locale) {
        return TextUtilsCompat.getLayoutDirectionFromLocale(new Locale(locale)) == ViewCompat.LAYOUT_DIRECTION_RTL ? true : false;
    }

// Set gravity to the right

private void repositionPasswordTextInArabic(){
        if( LocaleSettings.isRTL(LocaleSettings.getCurrentLanguage(this))){
            password.setGravity(Gravity.RIGHT);
        }
    }

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