【问题标题】:Edittext Fonts Are Not DisplayingEdittext 字体不显示
【发布时间】:2014-06-24 12:45:29
【问题描述】:

我遇到了一个奇怪的问题。

我创建了 CustomEdittext 类,用于为整个应用程序设置 Typeface,它几乎在所有情况下都能成功运行。

我正在使用circo.ttf

问题是当我设置android:inputType="textPassword"后,文字在输入后停止显示,可能是因为字体没有密码符号或可能有其他问题。

下面是我的问题的一个例子:

CustomEdittext.java

public class CustomEdittext extends EditText {

    public CustomEdittext(Context context) {
        super(context);
        changeFonts(context);

    }
    public CustomEdittext(Context context, AttributeSet attrs) {
        super(context, attrs);
        changeFonts(context);
    }

    public CustomEdittext(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        changeFonts(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
    }

    private void changeFonts(Context context) {
        // TODO Auto-generated method stub
        Typeface tface = Typeface.createFromAsset(context.getAssets(),"fonts/circo.ttf");
        this.setTypeface(tface);
        this.setTextColor(Color.parseColor("#921c50"));
        Log.i("Input Type", "Type : "+this.getInputType());
    }
}

login_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    android:orientation="vertical" 
    android:gravity="center_vertical">

    <com.equest.cwely.customviews.CustomTextview
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="LOGIN"
        android:textColor="@color/border_pink"
        android:textStyle="bold"
        android:gravity="center"
        android:padding="10dp"
        android:layout_marginTop="20dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <com.equest.cwely.customviews.CustomEdittext
        android:id="@+id/edt_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Username"
        android:singleLine="true"
        android:background="@drawable/edittext_bg"
        android:layout_marginTop="10dp"
        android:ems="10" >

        <requestFocus />
    </com.equest.cwely.customviews.CustomEdittext>

    // this is password field
    <com.equest.cwely.customviews.CustomEdittext
        android:id="@+id/edt_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Password"
        android:background="@drawable/edittext_bg"
        android:singleLine="true"
        android:layout_marginTop="10dp"
        android:inputType="textPassword" />

    <LinearLayout 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_marginTop="10dp"
        android:padding="10dp"
        android:orientation="vertical">

    <Button
        android:id="@+id/btn_login"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/border_pink"
        android:layout_margin="5dp"
        android:background="@drawable/button_bg"
        android:text="Login" />

    <Button
        android:id="@+id/btn_signup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_margin="5dp"
        android:textColor="@color/border_pink"
        android:background="@drawable/button_bg"
        android:text="Sign Up" />    
    </LinearLayout>

</LinearLayout>

LoginActivity.java

public class LoginActivity extends Activity {

    Button btn_login,btn_signup;
    EditText edt_username,edt_password;
    String result = "";
    String username = "",password = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_main);

        edt_username = (EditText)findViewById(R.id.edt_username);
        edt_password = (EditText)findViewById(R.id.edt_password);
        edt_password.setTransformationMethod(new PasswordTransformationMethod());
        btn_login = (Button)findViewById(R.id.btn_login);

        btn_login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                username = edt_username.getText().toString();
                password = edt_password.getText().toString();
                //new doLogin().execute();
            }
        });

        btn_signup = (Button)findViewById(R.id.btn_signup);
        btn_signup.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(intent);
            }
        });

    }

}

【问题讨论】:

  • 是的...帮我解决这个问题
  • 只是好奇,如果您使用不推荐使用的 android:password=true 而不是 inputType 会发生什么?
  • 不要在您的方法中设置颜色,而是尝试在 xml 文件中设置它。
  • 我已经测试了你的代码,我认为这个问题是由字体引起的,所以请尝试使用另一种字体并告诉我。
  • @MarkoNiciforovic : 使用android:password=true 没有任何区别.. 问题仍然存在..

标签: android android-edittext custom-controls android-typeface


【解决方案1】:

您包含的字体似乎不包含默认用于密码转换的符号的字符。你有两个选择:

  1. 使用不同的字体

  2. 将掩码字符更改为字体确实包含的内容。

因为我推测您不是退出者,所以这里有一些代码会将掩码字符从点 ( ) 更改为星号 ( * )。

首先你必须创建自己的PasswordTransformationMethod 来覆盖默认值:

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) {
            // This is where we make it an asterisk.  If you wish to use 
            // something else then change what this returns
            return '*'; 
        }
        public int length() {
            return mSource.length(); // Return default
        }
        public CharSequence subSequence(int start, int end) {
            return mSource.subSequence(start, end); // Return default
        }
    }
};

最后,您将新的转换方法设置为您希望屏蔽的EditText

edt_password = (EditText)findViewById(R.id.edt_password);
edt_password.setTransformationMethod(new AsteriskPasswordTransformationMethod());

我使用了来自this question的信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-06
    • 2014-09-26
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多