Android RTL password fields?

前端 未结 6 1641
别跟我提以往
别跟我提以往 2020-12-09 08:58

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

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

fields

相关标签:
6条回答
  • 2020-12-09 09:15

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

    android:textAlignment="viewStart"

    0 讨论(0)
  • 2020-12-09 09:15

    The only solution I've found was to set the gravity programatically to LEFT or RIGHT after setting the inputType.

    0 讨论(0)
  • 2020-12-09 09:15

    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;
            }
    
    0 讨论(0)
  • 2020-12-09 09:15

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

    }

    0 讨论(0)
  • 2020-12-09 09:20

    In My Case both worked fine.

    1) android:textAlignment="viewStart"

    And

    2)

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

    0 讨论(0)
  • 2020-12-09 09:24

    In my case, the problem was simply solved by changing the layout_width to wrap_content.

    0 讨论(0)
提交回复
热议问题