-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKeyTextView.java
85 lines (75 loc) · 1.95 KB
/
KeyTextView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.imhuayou.ui.widget;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.widget.TextView;
public class KeyTextView extends TextView {
String mStr;
List<Integer> starts;
List<Integer> ends;
List<Intent> intents;
public KeyTextView(Context context) {
super(context);
}
public KeyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public void setKeyText(String str, Intent intent) {
SpannableString text = new SpannableString(str);
if (starts != null && !starts.isEmpty()) {
for (int i = 0; i < starts.size(); i++) {
text.setSpan(new KeyTextClickableSpan(intents.get(i)),
starts.get(i), ends.get(i),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
setText(text);
setMovementMethod(LinkMovementMethod.getInstance());
}
public void show(){
SpannableString text = new SpannableString(mStr);
if (starts != null && !starts.isEmpty()) {
for (int i = 0; i < starts.size(); i++) {
text.setSpan(new KeyTextClickableSpan(intents.get(i)),
starts.get(i), ends.get(i),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
setText(text);
setMovementMethod(LinkMovementMethod.getInstance());
}
public void recycle(){
if(mStr != null){
mStr = null;
}
if (starts != null) {
starts = null;
ends = null;
intents = null;
}
}
public void addStr(String str, Intent intent) {
if (mStr == null) {
mStr = new String();
}
if (starts == null) {
starts = new ArrayList<Integer>();
ends = new ArrayList<Integer>();
intents = new ArrayList<Intent>();
}
if(intent != null){
starts.add(mStr.length());
mStr = mStr + str;
ends.add(mStr.length());
intents.add(intent);
}else{
mStr = mStr + str;
}
}
}