Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android编程中,如何将EditText中的特殊单词更改为特殊颜色?_Android - Fatal编程技术网

在android编程中,如何将EditText中的特殊单词更改为特殊颜色?

在android编程中,如何将EditText中的特殊单词更改为特殊颜色?,android,Android,如何在Android编程中将EditText的特殊单词更改为我的可选颜色 例如:蓝色文字在书写时变为蓝色;或将HTML单词改为绿色。如何操作?使用SpannableStringBuilder可以更改EditText或TextView String text = "Hello blue word"; String specialWord = "blue"; int specialWordIndex= text.indexOf(specialWord); SpannableStringBuilde

如何在Android编程中将EditText的特殊单词更改为我的可选颜色


例如:蓝色文字在书写时变为蓝色;或将HTML单词改为绿色。如何操作?

使用
SpannableStringBuilder
可以更改
EditText
TextView

String text = "Hello blue word";
String specialWord = "blue";
int specialWordIndex= text.indexOf(specialWord);

SpannableStringBuilder sb = new SpannableStringBuilder(text);
// Span to set text color to blue (or any color)
ForegroundColorSpan fcs = new ForegroundColorSpan(Color.BLUE);    
// Set text color from specialWord first index to specialWord last index
sb.setSpan(fcs, specialWordIndex, specialWordIndex+ specialWord.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

yourEditText.setText(sb);

在这类事情上使用SpannableString

@Override
    protected void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     SpannableString styledString
      = new SpannableString("Large\n\n"     // index 0 - 5
           + "Bold\n\n"          // index 7 - 11
           + "Underlined\n\n"    // index 13 - 23
           + "Italic\n\n"        // index 25 - 31
           + "Strikethrough\n\n" // index 33 - 46
           + "Colored\n\n"       // index 48 - 55
           + "Highlighted\n\n"   // index 57 - 68
           + "K Superscript\n\n" // "Superscript" index 72 - 83 
           + "K Subscript\n\n"   // "Subscript" index 87 - 96
           + "Url\n\n"           //  index 98 - 101
           + "Clickable\n\n");   // index 103 - 112

     // make the text twice as large
     styledString.setSpan(new RelativeSizeSpan(2f), 0, 5, 0);

     // make text bold
     styledString.setSpan(new StyleSpan(Typeface.BOLD), 7, 11, 0);

     // underline text
     styledString.setSpan(new UnderlineSpan(), 13, 23, 0);

     // make text italic
     styledString.setSpan(new StyleSpan(Typeface.ITALIC), 25, 31, 0);

     styledString.setSpan(new StrikethroughSpan(), 33, 46, 0);

     // change text color
     styledString.setSpan(new ForegroundColorSpan(Color.GREEN), 48, 55, 0);

     // highlight text
     styledString.setSpan(new BackgroundColorSpan(Color.CYAN), 57, 68, 0);

     // superscript
     styledString.setSpan(new SuperscriptSpan(), 72, 83, 0);
     // make the superscript text smaller
     styledString.setSpan(new RelativeSizeSpan(0.5f), 72, 83, 0);

     // subscript
     styledString.setSpan(new SubscriptSpan(), 87, 96, 0);
     // make the subscript text smaller
     styledString.setSpan(new RelativeSizeSpan(0.5f), 87, 96, 0);

     // url
     styledString.setSpan(new URLSpan("http://www.google.com"), 98, 101, 0);

     // clickable text
     ClickableSpan clickableSpan = new ClickableSpan() {

   @Override
   public void onClick(View widget) {
    // We display a Toast. You could do anything you want here.
    Toast.makeText(SpanExample.this, "Clicked", Toast.LENGTH_SHORT).show();

   }
  };

  styledString.setSpan(clickableSpan, 103, 112, 0);


  // Give the styled string to a TextView
  TextView textView = new TextView(this);

  // this step is mandated for the url and clickable styles.
  textView.setMovementMethod(LinkMovementMethod.getInstance());

  // make it neat
  textView.setGravity(Gravity.CENTER);
  textView.setBackgroundColor(Color.WHITE);

  textView.setText(styledString);

  setContentView(textView);

    }

来源:

如果您提供了一个示例,请使用spannable string(如果必须,请从链接复制),然后您可能会得到一个可接受的答案