[英]Recognize USSD code in textview in Android
有一個活動,我想設置TextView的 autoLink屬性。 我也想識別USSD代碼,例如* 140 * 7#,並在觸摸它時采取行動,例如電話號碼,網站網址,電子郵件地址。
[注意]通常,我想使用也可以識別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;
}
}
在此類中,重寫的setText()方法可識別任何模式,例如USSD代碼 。 還有一個名為TextLinkClickListener的接口,可以幫助我們調用識別USSD代碼 :
public interface TextLinkClickListener {
public void onTextLinkClick(View textView, String clickedString);
}
使用自定義類代替TextView:
<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代碼的地方,都應在下面的代碼中編寫並調用LinkEnabledTextView監聽器 :
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)));
}
哦,請注意清單文件中的寫入調用權限:
<uses-permission android:name="android.permission.CALL_PHONE" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.