提示密码EditText右侧的对齐方式 [英] Hint Alignment to the right of password EditText

查看:116
本文介绍了提示密码EditText右侧的对齐方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事阿拉伯语的活动.我希望用户名和密码的提示从右边开始,并且我键入从左边开始没有问题,但是在我的UI中,我希望提示在右边.但是当我为EditText添加inputType时,提示移到了左侧.我尝试以编程方式解决它,但是没有用.

I'm working on and activity with arabic language. I want the hint of the username and password to start from the right and I have no problem f typing started from the left but in my UI I want the hint to be of the right side. But when I'm adding the inputType for the EditText the hint moves to the left.I tried solving it programmatically but it didn't work.

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 4.4+中的EditText字段: https://code.google.com/p/android/issues/detail?id=201471 .截至2016年7月,该问题目前尚未解决.

This is a bug in the Android Framework, for EditText fields in Android 4.4+ : https://issuetracker.google.com/issues/37082815 or https://code.google.com/p/android/issues/detail?id=201471. As of July 2016, it is currently unsolved.

但是有一种解决方法:

  • 要使提示正确显示在右侧(从右到左/RTL模式),当出现以下情况时,您必须删除 InputType属性textPassword(InputType.TYPE_TEXT_VARIATION_PASSWORD)没有输入任何文字.

  • To make the hint display properly on the right (in right-to-left/RTL mode), you must remove the InputType property textPassword (InputType.TYPE_TEXT_VARIATION_PASSWORD), when there is no text entered.

要保留显示输入点以隐藏键入的文本的密码输入字段行为,必须在输入第一个字符时动态启用 InputType.TYPE_TEXT_VARIATION_PASSWORD.并且在输入第一个字符时必须将其重置所有字符都将被删除.

To retain the password entry field behaviour of showing dots to conceal typed text, you must dynamically enable InputType.TYPE_TEXT_VARIATION_PASSWORD, when the first character is entered in. And it must be reset when all characters are deleted.

为防止拉丁字符输入(如"abc123"之类的LTR文本)的UI跳至左侧或完全消失,您必须明确地将textDirection设置为RTL .

To prevent the UI glitch of Latin character input (LTR text like "abc123") jumping to the left or disappearing altogether, you must explicitly set textDirection to RTL.

以下是详细信息:

AndroidManifest.xml的先决条件:

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

您的XML布局包含:

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

具有解决方法错误修复的Java代码:

Java code with workaround bug fix:

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

它应该像这样工作:

这篇关于提示密码EditText右侧的对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆