让部分TextView里的文字可以点击并改变颜色

来源:互联网 发布:立体矢量图形软件 编辑:程序博客网 时间:2024/06/05 10:13

如题:


是百度到别人的并整合起来的。上图:



点击后效果:




开源使技术进步,上代码:


private void checkModel() {


  // =========查看模板========


  String str = join_look_model.getText().toString().trim();


  SpannableStringBuilder model = new SpannableStringBuilder(str);


  ForegroundColorSpan redSpan = new ForegroundColorSpan(


    Color.parseColor("#e45050"));


  Pattern ptn = Pattern.compile("(查看样板)");


  Matcher mth = ptn.matcher(str);


  while (mth.find()) {


   final String group = mth.group();


   ClickableSpan what = new ClickableSpan() {


    @Override


    public void onClick(View v) {


     //
     new AlertDialog.Builder(JoinActivity.this)


       .setMessage(group).setPositiveButton("ok", null)


       .show();


    }
   };
   model.setSpan(what, mth.start(), mth.end(),


     Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);


  }
  model.setSpan(redSpan, 18, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


  join_look_model.setText(model);


  join_look_model.setMovementMethod(LinkMovementMethod.getInstance());


  // ================================
 }




0 0