Android : Typeface is changed when i apply password Type on EditText

前端 未结 6 1070
借酒劲吻你
借酒劲吻你 2021-02-01 12:53

I use FloatLabel library (https://github.com/weddingparty/AndroidFloatLabel) to add a little animation when user begin to write something in an EditText Android

相关标签:
6条回答
  • 2021-02-01 13:27

    The following might solve it for you.

    pass.setTypeface(user.getTypeface());
    

    Essentially it just passes the Typeface of your username field and passes it as the Typeface for your password field.


    I found an explanation of this in the Dialogs API Guide.

    Tip: By default, when you set an EditText element to use the "textPassword" input type, the font family is set to monospace, so you should change its font family to "sans-serif" so that both text fields use a matching font style.

    In otherwords, a fix that can be done in XML would be as follows:

    <EditText
        android:id="@+id/password"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:hint="@string/password"/>
    
    0 讨论(0)
  • 2021-02-01 13:27

    Set the inputType as "text" in the xml,

    <EditText
        android:id="@+id/edtPassword"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/password"
        android:inputType="text"/>
    

    Then make the EditText in the activity as

    mEdtConfirmPassword = findViewById(R.id.edtPassword);
    mEdtConfirmPassword.setTransformationMethod(new PasswordTransformationMethod());
    
    0 讨论(0)
  • 2021-02-01 13:27

    It may be late to answer this question but i have came across this problem. So thought to answer this. I have solved it by Implementing a CustomTextWatcher.

    Here is the snippet

    private class CustomTextWatcher implements TextWatcher {
        private EditText mEditText;
    
        public CustomTextWatcher(EditText e) { 
            mEditText = e;
            mEditText.setTypeface(Typeface.DEFAULT);
        }
    
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            mEditText.setTypeface(Typeface.DEFAULT);
        }
    
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mEditText.setTypeface(Typeface.MONOSPACE);
        }
    
        public void afterTextChanged(Editable s) { 
            if(s.length() <= 0){
                mEditText.setTypeface(Typeface.DEFAULT);
            } else {
                mEditText.setTypeface(Typeface.MONOSPACE);
            }
    
    
        }
    }
    

    Here is how you can use it in your application

    EditText pin = (EditText) findViewById(R.id.pin);
    pin.addTextChangedListener(new CustomTextWatcher(pin));
    
    0 讨论(0)
  • 2021-02-01 13:29

    For some reason I didn't have to set the transformation method so this may be a better solution. In MyActivity:

      EditText editText_password = findViewById(R.id.edittext);
        editText_password.setTransformationMethod(new PasswordTransformationMethod());
    
    0 讨论(0)
  • 2021-02-01 13:30

    you can use android:hit, and setHitColor in your activity, it will not affect the normal input

    0 讨论(0)
  • 2021-02-01 13:33

    Another solution is just to save the typeface before you change the inputType and reapply it after. Strangely this doesn't happen when set the inputType in xml but happens when setting programatically.

    val typeface = editText.typeface
    editText.inputType = inputType
    editText.typeface = typeface
    
    0 讨论(0)
提交回复
热议问题