代码之家  ›  专栏  ›  技术社区  ›  Sid

文本中大写/小写字母和数字的不同颜色查看

  •  0
  • Sid  · 技术社区  · 9 年前

    我的应用程序中有一个textView,我正在用一个一次性密码填充它,密码中包含大写/小写字母和数字。我现在有一个要求,所有三种类型的字符都要有不同的颜色,以提高可读性。在谷歌上四处寻找,但可以得到很多。有人能告诉我正确的方法吗。会从那里捡起来。 提前谢谢!!

    3 回复  |  直到 9 年前
        1
  •  3
  •   Community Mohan Dere    8 年前

    试试这个:

    主活动.java

    public class MainActivity extends Activity
    {
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            TextView tv = (TextView) findViewById(R.id.myColoredString);
            String x = tv.getText().toString();
            SpannableStringBuilder builder = new SpannableStringBuilder();
    
            for(int i = 0; i< x.length();i++){
    
                //UpperCase : RED
                if(x.charAt(i) >= 65 && x.charAt(i) <= 90) {
                    String s = x.charAt(i) + "";
                    SpannableString spannableString = new SpannableString(s);
                    spannableString.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
                    builder.append(spannableString);
                }
    
                //LowerCase: GREEN
                if(x.charAt(i) >= 97 && x.charAt(i) <= 122) {
                    String s = x.charAt(i) + "";
                    SpannableString spannableString = new SpannableString(s);
                    spannableString.setSpan(new ForegroundColorSpan(Color.GREEN), 0, s.length(), 0);
                    builder.append(spannableString);
                }
    
                //Digits: BLUE
                if(x.charAt(i) >= 48 && x.charAt(i) <= 57) {
                    String s = x.charAt(i) + "";
                    SpannableString spannableString = new SpannableString(s);
                    spannableString.setSpan(new ForegroundColorSpan(Color.BLUE), 0, s.length(), 0);
                    builder.append(spannableString);
                }
            }
    
            tv.setText(builder, TextView.BufferType.SPANNABLE);
    
    
        }
    }
    

    活动_主.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
        <TextView
           android:id="@+id/myColoredString"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="HeLlo98"
        />
    </RelativeLayout>
    

    Refer this link

        2
  •  2
  •   Community Mohan Dere    8 年前

    试试这个答案,

     TextView TV = (TextView)findViewById(R.id.tv_onetime_password);
    String oneTimePass="43434Qe";
    for(int i = 0; i < oneTimePass.length(); i++) {
    
    String myChar=oneTimePass.charAt(i);
    
     Spannable word = new SpannableString(myChar);
    
    //for digits
     if (Character.isLetter(oneTimePass.charAt(i))){
    word.setSpan(new ForegroundColorSpan(Color.WHITE), i,(i+1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     TV.append(word);
    
    }
    
    //for letters
    else{
    boolean hasUppercase = !myChar.equals(myChar.toLowerCase());
    
    if(hasUppercase){
     word.setSpan(new ForegroundColorSpan(Color.BLUE), i,(i+1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     TV.append(word);
    }
    
    else {
     word.setSpan(new ForegroundColorSpan(Color.RED), i,(i+1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     TV.append(word);
    }
    }       
    
    }

    更多参考, Single TextView with multiple colored text

    注:未测试输出。。

        3
  •  0
  •   Guru_VL    9 年前

    您提到了它的密码字段,因此它不应显示我们输入的任何文本。但是,如果你真的想拥有不同的风格/颜色来进行不同的粉碎,那么你可以尝试

    1. 首先找出根据您的要求分组的字符(数字、大写和小写)

    2. 使用Html为每组设置颜色。fromHtml方法。

    字符串字符串=大写。a小写1数字。“;

    您也可以尝试Spanable字符串。