简体   繁体   English

Android RTL密码字段?

[英]Android RTL password fields?

It appears that if you have an EditText on android with the看来,如果你在 android 上有一个 EditText

android:inputType="textPassword" or android:password="true android:inputType="textPassword"android:password="true

fields on them, right-to-left text does NOT appear right-to-left (stays left-to-right).字段,从右到左的文本不会从右到左显示(保持从左到右)。

However without the password denotations the text does appear RTL.但是,如果没有密码符号,文本确实会出现 RTL。

Is this a known issue or is there a workaround?这是一个已知问题还是有解决方法?

For 17+ (4.2.x+) you can use textAlignment对于 17+ (4.2.x+) 你可以使用textAlignment

android:textAlignment="viewStart" android:textAlignment="viewStart"

我发现的唯一解决方案是在设置 inputType 后以编程方式将重力设置为 LEFT 或 RIGHT。

在我的情况下,问题是通过将layout_width更改为wrap_content来解决的。

If you put inputType = textPassword or set a passwordTransformation method on EditText, text direction is taken as LTR.如果在 EditText 上设置 inputType = textPassword 或设置 passwordTransformation 方法,则文本方向将被视为 LTR。 It means RTL for password is discouraged.这意味着不鼓励密码的 RTL。 You need to write custom TextView to override this behaviour.您需要编写自定义 TextView 来覆盖此行为。

Code snippet from android source for TextView.来自 android 源代码的 TextView 代码片段。

// PasswordTransformationMethod always have LTR text direction heuristics returned by
        // getTextDirectionHeuristic, needs reset
        mTextDir = getTextDirectionHeuristic();


protected TextDirectionHeuristic getTextDirectionHeuristic() {
        if (hasPasswordTransformationMethod()) {
            // passwords fields should be LTR
            return TextDirectionHeuristics.LTR;
        }

In My Case both worked fine.在我的案例中,两者都运行良好。

1) android:textAlignment="viewStart" 1) android:textAlignment="viewStart"

And并且

2) 2)

https://stackoverflow.com/a/38291472/6493661 https://stackoverflow.com/a/38291472/6493661

and the right answer is:正确答案是:

RtlEditText mUserPassword = root.findViewById(R.id.register_fragment_password);
mUserPassword.setTransformationMethod(new AsteriskPasswordTransformationMethod());

creating our own EditText!创建我们自己的 EditText!

it work prefectly only if you replace the dot with astrix by AsteriskPasswordTransformationMethod below this code.仅当您通过此代码下方的 AsteriskPasswordTransformationMethod 用星号替换点时,它才能完美地工作。

public class RtlEditText extends EditText {


public RtlEditText(Context context) {
    super(context);
}

public RtlEditText(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

public RtlEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public RtlEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
public TextDirectionHeuristic getTextDirectionHeuristic() {
        // passwords fields should be LTR
        return TextDirectionHeuristics.ANYRTL_LTR;
}}



public class AsteriskPasswordTransformationMethod extends PasswordTransformationMethod {
@Override
public CharSequence getTransformation(CharSequence source, View view) {
    return new PasswordCharSequence(source);
}

private class PasswordCharSequence implements CharSequence {
    private CharSequence mSource;
    public PasswordCharSequence(CharSequence source) {
        mSource = source; // Store char sequence
    }
    public char charAt(int index) {
        return '*'; // This is the important part
    }
    public int length() {
        return mSource.length(); // Return default
    }
    public CharSequence subSequence(int start, int end) {
        return mSource.subSequence(start, end); // Return default
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM