密码EditText右对齐的提示文本

10

我正在处理一项阿拉伯语活动。我希望用户名和密码的提示从右侧开始,如果输入从左侧开始也没有问题,但在我的UI中,我希望提示位于右侧。但是当我为EditText添加inputType时,提示会向左移动。我尝试了编程解决它,但没有成功。

Java

    EditText password = (EditText) findViewById(R.id.input_password);
    password.setTypeface(Typeface.DEFAULT);

XML

<EditText
            android:id="@+id/input_password"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="كلمة المرور"
            android:textColorHint="#FFFFFF"
            android:inputType="textPassword"
            android:background="@null"
            android:textColor="#FFFFFF"
            android:textSize="20dp"/>

4
可能是右对齐EditText Android的重复问题。 - Amsheer
8个回答

9
这是 Android Framework 的一个错误,适用于 Android 4.4+ 中的 EditText 字段:https://issuetracker.google.com/issues/37082815https://code.google.com/p/android/issues/detail?id=201471。截至 2016 年 7 月,该问题尚未解决。
然而,有一种解决方法:
- 要使提示在右侧正确显示(从右到左/RTL 模式),必须在没有输入文本时删除 InputType 属性 textPasswordInputType.TYPE_TEXT_VARIATION_PASSWORD)。 - 要保留密码输入字段的行为以显示点以隐藏已键入的文本,必须在第一个字符输入后动态启用 InputType.TYPE_TEXT_VARIATION_PASSWORD。并在删除所有字符时重置它。 - 为了防止 Latin 字符输入(LTR 文本,如“abc123”)跳到左侧或完全消失的 UI 故障,必须明确设置 textDirection 为 RTL
以下是详细信息: AndroidManifest.xml 的先决条件:
<application
    ...
    android:supportsRtl="true"
    ... >
</application>

您的XML布局包含:

     <EditText
        android:id="@+id/password"
        android:inputType="textPassword"
        android:hint="סיסמא"
        ... />

带有解决Bug的Java代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.login_fragment_layout, container, false);
    final EditText password = (EditText) view.findViewById(R.id.password);

    // Workaround https://issuetracker.google.com/issues/37082815 for Android 4.4+
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && isRTL(getActivity())) {

        // Force a right-aligned text entry, otherwise latin character input,
        // like "abc123", will jump to the left and may even disappear!
        password.setTextDirection(View.TEXT_DIRECTION_RTL);

        // Make the "Enter password" hint display on the right hand side
        password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    }

    password.addTextChangedListener(new TextWatcher() {

        boolean inputTypeChanged;

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

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {}

        @Override
        public void afterTextChanged(Editable s) {

            // Workaround https://code.google.com/p/android/issues/detail?id=201471 for Android 4.4+
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && isRTL(getActivity())) {
                if (s.length() > 0) {
                    if (!inputTypeChanged) {

                        // When a character is typed, dynamically change the EditText's
                        // InputType to PASSWORD, to show the dots and conceal the typed characters.
                        password.setInputType(InputType.TYPE_CLASS_TEXT |
                                InputType.TYPE_TEXT_VARIATION_PASSWORD |
                                InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

                        // Move the cursor to the correct place (after the typed character)
                        password.setSelection(s.length());

                        inputTypeChanged = true;
                    }
                } else {
                    // Reset EditText: Make the "Enter password" hint display on the right
                    password.setInputType(InputType.TYPE_CLASS_TEXT |
                            InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

                    inputTypeChanged = false;
                }
            }
        }
    });

    return view;
}

public static boolean isRTL(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return context.getResources().getConfiguration().getLayoutDirection()
                == View.LAYOUT_DIRECTION_RTL;
        // Another way:
        // Define a boolean resource as "true" in res/values-ldrtl
        // and "false" in res/values
        // return context.getResources().getBoolean(R.bool.is_right_to_left);
    } else {
        return false;
    }
}

应该像这样工作:

视频截图,右侧提示,右侧输入密码


8
使用Gravity属性调整EditText的提示
        <EditText
        android:id="@+id/input_password"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="كلمة المرور"
        android:textColorHint="#FFFFFF"
        android:gravity="right"
        android:inputType="textPassword"
        android:background="@null"
        android:textColor="#FFFFFF"
        android:textSize="20dp"/>

6

这个对我来说很好用

android:textAlignment="viewStart"

提示方向将在正确的一侧


4

增加重力 向右 尝试这种方法

<EditText
            android:id="@+id/input_password"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="كلمة المرور"
            android:textColorHint="#FFFFFF"
            android:inputType="textPassword"
            android:background="@null"
            android:textColor="#FFFFFF"
            android:textSize="20dp"
            android:gravity="right"/>

如果您的API级别在17及以上,则可以使用。
android:textDirection="anyRtl"

3

对于AppCompatEditText,我发现将文本重心设置为开头是有效的

<androidx.appcompat.widget.AppCompatEditText
        ......
        android:hint="كلمه السر"
        android:inputType="textPassword"
        android:gravity="start"
       .............../>

仅适用于api 16以上的版本

您还可以尝试,必须存储您当前的语言,我使用了这个库 com.akexorcist:localizationactivity

if(currentLanguage.country.toLowerCase() == "arabic"){
        etPassword.gravity = GravityCompat.END
}else{
        etPassword.gravity = GravityCompat.START
}

0
使用“gravity”属性:
<EditText
    android:id="@+id/input_password"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:hint="كلمة المرور"
    android:textColorHint="#FFFFFF"
    android:gravity="right"
    android:inputType="textPassword"
    android:background="@null"
    android:textColor="#FFFFFF"
    android:textSize="20dp"/>

0

我认为你可以通过在希伯来语/阿拉伯语(或任何其他从右到左的语言)文本中添加\u202B来修复它。

例如:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <EditText android:hint="Enter a number..." android:id="@+id/editText"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" android:inputType="number"/>

    <EditText android:hint="\u202Bהכנס מספר..." android:id="@+id/editText2"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" android:inputType="number"/>
</LinearLayout>

很遗憾,似乎无法在布局预览中显示,但是在真实设备上却有效。关于此问题,我在这里写了一篇文章链接

0

这是我针对类型为密码的editText提出的解决方案(变通方法)。适用于Android 5和Android 6。

edittext_password = (EditText) rootView.findViewById(R.id.edittext_password);
            if (RTLUtils.isLeftToRightLanguage()) {
                edittext_password.setGravity(Gravity.START);
            } else {
                // Force a right-aligned text entry, otherwise latin character input,
                // like "abc123", will jump to the left and may even disappear!
                edittext_password.setTextDirection(View.TEXT_DIRECTION_RTL);
                // Make the hint display on the right hand side
                edittext_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
            }
            edittext_password.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    isLeftToRight = RTLUtils.isLeftToRightLanguage(charSequence.toString());
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    if (editable.length() > 0) {
                        isLeftToRight = RTLUtils.isLeftToRightLanguage(editable.toString());
                        if (isLeftToRight) {
                            edittext_password.setGravity(Gravity.START);
                            edittext_password.setTextDirection(View.TEXT_DIRECTION_LTR);
                            edittext_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
                            // Move the cursor to the correct place (after the typed character)
                            edittext_password.setSelection(editable.length());
                        } else {
                            //edittext_password.setGravity(Gravity.END);
                            edittext_password.setTextDirection(View.TEXT_DIRECTION_RTL);
                            // When a character is typed, dynamically change the EditText's
                            // InputType to PASSWORD, to show the dots and conceal the typed characters.
                            edittext_password.setInputType(InputType.TYPE_CLASS_TEXT |
                                    InputType.TYPE_TEXT_VARIATION_PASSWORD |
                                    InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
                            // Move the cursor to the correct place (after the typed character)
                            edittext_password.setSelection(editable.length());
                        }
                    } else { // empty text
                        if (!RTLUtils.isLeftToRightLanguage()) {
                            // Must be in this order (first setInputType then setTextDirection)
                            edittext_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
                            edittext_password.setTextDirection(View.TEXT_DIRECTION_RTL);
                        }
                    }
                }
            });

  public static boolean isLeftToRightLanguage() {
        Bidi bidi = new Bidi(Locale.getDefault().getDisplayLanguage(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
        if (bidi.isLeftToRight()) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isLeftToRightLanguage(String text) {
        Bidi bidi = new Bidi(text, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
        if (bidi.isLeftToRight()) {
            return true;
        } else {
            return false;
        }
    }

这是结果: (阿拉伯软键盘)

enter image description here

以及英文软键盘:

enter image description here


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