简体   繁体   中英

Android RTL password fields?

It appears that if you have an EditText on android with the

android:inputType="textPassword" or 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.

Is this a known issue or is there a workaround?

For 17+ (4.2.x+) you can use textAlignment

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. It means RTL for password is discouraged. You need to write custom TextView to override this behaviour.

Code snippet from android source for 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"

And

2)

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!

it work prefectly only if you replace the dot with astrix by AsteriskPasswordTransformationMethod below this code.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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