提示对齐密码EditText的右侧

问题描述 投票:5回答:6

我正在研究和使用阿拉伯语进行活动。我希望用户名和密码的提示从右侧开始,我没有问题从左侧开始输入,但在我的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"/>
android android-edittext right-to-left
6个回答
7
投票

这是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。并且必须在删除所有字符时重置它。
  • 为了防止拉丁字符输入的UI故障(像“abc123”这样的LTR文本)向左跳转或完全消失,必须将textDirection显式设置为RTL。

以下是详细信息:

你的qazxsw poi的先决条件:

AndroidManifest.xml

您的XML布局包含:

<application
    ...
    android:supportsRtl="true"
    ... >
</application>

修复了变通方法的Java代码:

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

它应该像这样工作:

@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; } }


5
投票

使用“重力属性”调整EditText的提示

Video screenshot, Arabic: Hint on right, password typed on right

1
投票

加入引力 <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"/> 尝试这种方式

right

如果您的API级别为17或更高,则可以使用

<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"/>

0
投票

使用'gravity'属性:

android:textDirection="anyRtl"

0
投票

我想你可以通过将<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"/> 添加到希伯来语/阿拉伯语(或任何其他RTL语言)文本来解决它。

例:

\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
投票

对于AppCompatEditText,编辑文本重力开始为我工作

here

这只适用于api 16

你也可以尝试,你必须存储当前语言的地方我用过这个库com.akexorcist:localizationactivity

<androidx.appcompat.widget.AppCompatEditText
        ......
        android:hint="كلمه السر"
        android:inputType="textPassword"
        android:gravity="start"
       .............../>
© www.soinside.com 2019 - 2024. All rights reserved.