Android TextView part of the text to achieve the click event

IS at The class text for the this Whose Content and Markup both-CAN BE changed.
(This is a content and markup can change the text of the class)
quickly realize
direct look at the code:


@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = findViewById(R.id.tvContent); final SpannableStringBuilder style = new SpannableStringBuilder(); //设置文字 style.append("注册即为同意《某某某协议》"); //设置部分文字点击事件 ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View widget) { Toast.makeText(MainActivity.this, "触发点击事件!", Toast.LENGTH_SHORT).show(); } }; style.setSpan(clickableSpan, 7, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tv.setText(style); //设置部分文字颜色 ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#0000FF")); style.setSpan(foregroundColorSpan, 7, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //配置给TextView tv.setMovementMethod(LinkMovementMethod.getInstance()); tv.setText(style); }


Brief SpannableStringBuilder, this class is actually your TextView text in a simple configuration, after configuring the properties you want, directly call the following code:

How // method to set the cursor measurement
textView.setMovementMethod (LinkMovementMethod.getInstance ());
// assigned to the TextView
textView.setText (style)
how to configure each attribute it? We set the font color, for example:

// Set the text color section

// Create the font color Span, and initializes the font color attribute
ForegroundColorSpan foregroundColorSpan = new new ForegroundColorSpan (Color.parseColor ( "# 0000FF"));
// we set the first 7 to 13 characters in the middle of the blue
style.setSpan (foregroundColorSpan , 7, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

SpannableStringBuilder and SpannableString primarily through the use of setSpan (Object what, int start, int end, int flags) to change the text style.

setSpan () method corresponding to the following parameters:

start: Span specified starting position
end: Specifies the end position Span does not include this location.
flags: the following four values
Spannable SPAN_INCLUSIVE_EXCLUSIVE:. including front, back is not included, i.e. insert new text style is applied prior to the text, and insert new text in the text does not apply the style
Spannable SPAN_INCLUSIVE_INCLUSIVE:. foregoing comprising , including the back, i.e., the text is inserted before the new text will apply the style, the new text will be inserted in the text after the style applied
Spannable SPAN_EXCLUSIVE_EXCLUSIVE:. does not include the front, back does not include
Spannable SPAN_EXCLUSIVE_INCLUSIVE:. does not include the front, back comprising
detailed implementation have already traveled [Android] powerful SpannableStringBuilder @ with the mood there is very clear explanation, we went home inspection to the original author.



 

Guess you like

Origin www.cnblogs.com/Alex80/p/11823976.html