EDITTEXT字体不显示 [英] Edittext Fonts Are Not Displaying

查看:286
本文介绍了EDITTEXT字体不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历一个奇怪的问题。

我创建的 CustomEdittext类设置字样为整个应用程序,它在几乎所有情况下成功的作品。

我使用 circo.ttf

问题是,当我设置的 的android:inputType下=textPassword ,文字停止输入后显示,也许是因为字体没有按'吨有密码符号或者有一些其他的问题。

下面是我的问题的例子:

CustomEdittext.java

 公共类CustomEdittext扩展的EditText {    公共CustomEdittext(上下文的背景下){
        超级(上下文);
        changeFonts(上下文);    }
    公共CustomEdittext(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        changeFonts(上下文);
    }    公共CustomEdittext(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
        changeFonts(上下文);
    }    @覆盖
    保护无效的onDraw(帆布油画){
        // TODO自动生成方法存根
        super.onDraw(画布);
    }    私人无效changeFonts(上下文的背景下){
        // TODO自动生成方法存根
        字体tface = Typeface.createFromAsset(context.getAssets(),字体/ circo.ttf);
        this.setTypeface(tface);
        this.setTextColor(Color.parseColor(#921c50));
        Log.i(输入型,类型:+ this.getInputType());
    }
}

login_main.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:填充=20dp
    机器人:方向=垂直
    机器人:重力=center_vertical>    < com.equest.cwely.customviews.CustomTextview
        机器人:ID =@ + ID / textView1
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=LOGIN
        机器人:文字颜色=@彩色/ border_pink
        机器人:文字样式=大胆
        机器人:比重=中心
        机器人:填充=10dp
        机器人:layout_marginTop =20dp
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?    < com.equest.cwely.customviews.CustomEdittext
        机器人:ID =@ + ID / edt_username
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:提示=用户名
        机器人:单线=真
        机器人:背景=@绘制/ edittext_bg
        机器人:layout_marginTop =10dp
        机器人:EMS =10>        < requestFocus的/>
    < /com.equest.cwely.customviews.CustomEdittext>    //这是密码字段
    < com.equest.cwely.customviews.CustomEdittext
        机器人:ID =@ + ID / edt_password
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:EMS =10
        机器人:提示=密码
        机器人:背景=@绘制/ edittext_bg
        机器人:单线=真
        机器人:layout_marginTop =10dp
        安卓的inputType =textPassword/>    <的LinearLayout
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =FILL_PARENT
        机器人:layout_marginTop =10dp
        机器人:填充=10dp
        机器人:方向=垂直>    <按钮
        机器人:ID =@ + ID / btn_login
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =1
        机器人:文字颜色=@彩色/ border_pink
        机器人:layout_margin =5DP
        机器人:背景=@绘制/ button_bg
        机器人:文字=登录/>    <按钮
        机器人:ID =@ + ID / btn_signup
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =1
        机器人:layout_margin =5DP
        机器人:文字颜色=@彩色/ border_pink
        机器人:背景=@绘制/ button_bg
        机器人:文字=注册/>
    < / LinearLayout中>< / LinearLayout中>

LoginActivity.java

 公共类LoginActivity延伸活动{    按钮btn_login,btn_signup;
    的EditText edt_username,edt_password;
    字符串结果=;
    字符串username =,密码=;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根
        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(新PasswordTransformationMethod());
        btn_login =(按钮)findViewById(R.id.btn_login);        btn_login.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){                。用户名= edt_username.getText()的toString();
                。密码= edt_password.getText()的toString();
                //新doLogin()执行();
            }
        });        btn_signup =(按钮)findViewById(R.id.btn_signup);
        btn_signup.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                意向意图=新意图(getApplicationContext(),RegisterActivity.class);
                startActivity(意向);
            }
        });    }}


解决方案

看来你已经包含的字体不包含默认密码变换使用的符号的字符。你有两个选择:


  1. 使用不同的字体


  2. 面膜性状改变的东西,字体确实包含。


由于我猜测你不是一个半途而废的人,这里的一些code,将面具人物从一个点变化(),以星号(*)。

首先,你必须让自己的 PasswordTransformationMethod 覆盖默认的:

公共类AsteriskPasswordTransformationMethod扩展PasswordTransformationMethod {
    @覆盖
    公众的CharSequence getTransformation(CharSequence的源,查看查看){
        返回新PasswordCharSequence(源);
    }    私有类PasswordCharSequence实现的CharSequence {
        私人CharSequence的mSource;
        公共PasswordCharSequence(CharSequence的源){
            mSource =来源; //保存字符序列
        }
        公共字符的charAt(INT指数){
            //这是我们让一个星号。如果您希望使用
            //别的东西再改一下这个回报
            返回'*';
        }
        公众诠释长度(){
            返回mSource.length(); //返回默认
        }
        公共CharSequence的子进程(INT开始,诠释完){
            返回mSource.subSequence(起点,终点); //返回默认
        }
    }
};

最后,你设置新的变换方法对的EditText 你想掩盖。

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

我使用<一个信息href=\"http://stackoverflow.com/questions/14051962/change-edittext-password-mask-character-to-asterisk\">this问题。

I'm going through a weird problem.

I have created CustomEdittext class for setting Typeface for whole application and it works successfully in nearly all cases.

I am using circo.ttf

The problem is that when I set android:inputType="textPassword", text stops displaying after typing, maybe because the font doesn't have a password symbol or maybe there is some other problem.

Below is an example of my issue :

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

    }

}

解决方案

It seems that the font that you have included does not contain a character for the symbol used by default for password transformation. You have two options:

  1. Use a different font

  2. Change the mask character to something that the font does contain.

Because I speculate you're not a quitter, here's some code that will change the mask character from a dot ( ) to an asterisk ( * ).

First you must make your own PasswordTransformationMethod to override the default:

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

Finally you set your new transformation method to the EditText you wish to mask.

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

I used information from this question.

这篇关于EDITTEXT字体不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆