【问题标题】:Android : Typeface is changed when i apply password Type on EditTextAndroid:当我在 EditText 上应用密码类型时,字体会更改
【发布时间】:2014-07-29 19:26:56
【问题描述】:

当用户开始在 EditText Android 中写东西时,我使用 FloatLabel 库 (https://github.com/weddingparty/AndroidFloatLabel) 添加一个小动画。

我的问题是,当我将密码类型应用于我的 EditText 时,字体似乎发生了变化。我想保持和平常一样的字体。 (见图1)

但是当我添加以下行来应用密码类型时,提示的字体似乎改变了!

pass.getEditText().setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

【问题讨论】:

    标签: android android-edittext typeface


    【解决方案1】:

    以下可能会为您解决。

    pass.setTypeface(user.getTypeface());
    

    基本上它只是传递您的用户名字段的Typeface 并将其作为您的密码字段的Typeface 传递。


    我在Dialogs API Guide 中找到了对此的解释。

    提示:默认情况下,当您将 EditText 元素设置为使用 "textPassword"输入类型,字体系列设置为等宽,所以你 应将其字体系列更改为"sans-serif",以便两个文本字段 使用匹配的字体样式。

    换句话说,可以在 XML 中完成的修复如下:

    <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"/>
    

    【讨论】:

    • fontFamily 在 api 级别 16 中引入! 2.2 是什么?
    • 这正是解决方案。无需在代码中使用 setTypeface() 和 setTransformationMethod() 进行设置,因为您键入的下一个字母被隐藏且不显示,它们甚至无法完全工作
    • user 是什么?
    【解决方案2】:

    由于某种原因,我不必设置转换方法,所以这可能是一个更好的解决方案。 在我的活动中:

      EditText editText_password = findViewById(R.id.edittext);
        editText_password.setTransformationMethod(new PasswordTransformationMethod());
    

    【讨论】:

    • 这是使用自定义字体时唯一对我有用的方法 - 将 inputType 设置回文本,然后设置转换
    【解决方案3】:

    你可以在activity中使用android:hit和setHitColor,不会影响正常输入

    【讨论】:

      【解决方案4】:

      在xml中设置inputType为“text”,

      <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"/>
      

      然后将activity中的EditText设为

      mEdtConfirmPassword = findViewById(R.id.edtPassword);
      mEdtConfirmPassword.setTransformationMethod(new PasswordTransformationMethod());
      

      【讨论】:

        【解决方案5】:

        回答这个问题可能已经晚了,但我遇到了这个问题。所以想回答这个问题。我已经通过Implementing a CustomTextWatcher 解决了。

        这里是sn-p

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

        这里是你如何在你的应用程序中使用它

        EditText pin = (EditText) findViewById(R.id.pin);
        pin.addTextChangedListener(new CustomTextWatcher(pin));
        

        【讨论】:

          【解决方案6】:

          另一种解决方案是在更改 inputType 之前保存 typeface 并在之后重新应用。奇怪的是,在 xml 中设置 inputType 时不会发生这种情况,而是在以编程方式设置时发生。

          val typeface = editText.typeface
          editText.inputType = inputType
          editText.typeface = typeface
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-06-01
            • 1970-01-01
            • 1970-01-01
            • 2013-02-24
            • 2012-12-12
            • 2017-08-25
            • 2011-12-25
            • 1970-01-01
            相关资源
            最近更新 更多