在Android的textview中识别USSD代码

问题描述:

有一个活动,我想要设置autoLink属性TextView。此外,我想识别USSD代码* 140 * 7#并在触摸它时采取行动,例如电话号码,网站网址,电子邮件地址。在Android的textview中识别USSD代码

[注意]一般来说,我想使用自定义TextView也可以识别USSD代码。

enter image description here

我一直在谷歌上搜索,但我无法找到一个合适的解决方案。

+0

也许你可以为USSD代码创建一个正则表达式,并用文本匹配它的' TextView' –

+0

当我可以在textview中匹配USSD代码时,我该怎么办?我想让它显示为蓝色的电话号码。 –

+1

您可以在使用'setText()'方法之前检查并设置PaintFlags为下划线并使其颜色为蓝色。或者,您可以扩展'TextView'并覆盖'onDraw()'方法并在那里检查 –

最后,我可以找到我的请求的解决方案。如在@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))); 
} 

哦,在清单文件知道写调用permissiion:

<uses-permission android:name="android.permission.CALL_PHONE" />