我想设置一个具有 autoLink 属性的 TextView。同时,我还想识别像 *140*7# 这样的 USSD代码 ,并在触摸时执行相应操作,例如电话号码、网站URL和电子邮件地址。
[注意] 一般来说,我希望使用一个可以识别 USSD 代码的自定义 TextView。
我已经搜索过了,但是没有找到合适的解决方案。
终于,我找到了解决我的请求的方法。正如@basant_matharu在评论中提到的那样,我扩展了TextView元素,你可以在下面的代码中看到:
public class LinkEnabledTextView extends TextView {
// Pattern for gathering *140*1# from the Text
Pattern ussdPattern = Pattern.compile("(\\*[0-9]+[\\*[0-9]+]*#)");
private TextLinkClickListener mListener;
private ArrayList<Hyperlink> listOfLinks;
public LinkEnabledTextView(Context context, AttributeSet attrs) {
super(context, attrs);
listOfLinks = new ArrayList<Hyperlink>();
}
public void setText(String text) {
SpannableString linkableText = new SpannableString(text);
gatherLinks(listOfLinks, linkableText, ussdPattern);
for (Hyperlink linkSpec : listOfLinks) {
// this process here makes the Clickable Links from the text
linkableText.setSpan(linkSpec.span, linkSpec.start, linkSpec.end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
// sets the text for the TextView with enabled links
super.setText(linkableText);
}
public void setOnTextLinkClickListener(TextLinkClickListener newListener) {
mListener = newListener;
}
private void gatherLinks(ArrayList<Hyperlink> links, Spannable s, Pattern pattern) {
Matcher m = pattern.matcher(s);
while (m.find()) {
int start = m.start();
int end = m.end();
Hyperlink spec = new Hyperlink();
spec.textSpan = s.subSequence(start, end);
spec.span = new InternalURLSpan(spec.textSpan.toString());
spec.start = start;
spec.end = end;
links.add(spec);
}
}
public interface TextLinkClickListener {
public void onTextLinkClick(View textView, String clickedString);
}
/**
* Class for storing the information about the Link Location
*/
public class InternalURLSpan extends ClickableSpan {
private String clickedSpan;
public InternalURLSpan(String clickedString) {
clickedSpan = clickedString;
}
@Override
public void onClick(View textView) {
mListener.onTextLinkClick(textView, clickedSpan);
}
}
class Hyperlink {
CharSequence textSpan;
InternalURLSpan span;
int start;
int end;
}
}
public interface TextLinkClickListener {
public void onTextLinkClick(View textView, String clickedString);
}
<com.example.test.custom_textview.LinkEnabledTextView
android:id="@+id/txtMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@android:color/black"
android:autoLink="all"/>
LinkEnabledTextView textView = (LinkEnabledTextView) findViewById(R.id.txtMessage);
textView.setOnTextLinkClickListener(this);
textView.setText(text);
在您想要调用USSD代码的任何地方,您都应编写以下代码并调用:
public void onTextLinkClick(View textView, String clickedString) {
String ussdCode = clickedString.substring(0, clickedString.indexOf("#")) + Uri.encode("#");
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + ussdCode)));
}
哦,注意在Manifest文件中写入调用权限:
<uses-permission android:name="android.permission.CALL_PHONE" />
setText()
方法之前检查并将PaintFlags设置为下划线,并将颜色设为蓝色。或者,你可以扩展TextView
并重写onDraw()
方法,在那里进行检查。 - Basant Singh