在Android中识别文本视图中的USSD代码

3

我想设置一个具有 autoLink 属性的 TextView。同时,我还想识别像 *140*7# 这样的 USSD代码 ,并在触摸时执行相应操作,例如电话号码、网站URL和电子邮件地址。

[注意] 一般来说,我希望使用一个可以识别 USSD 代码的自定义 TextView

enter image description here

我已经搜索过了,但是没有找到合适的解决方案。


也许你可以为USSD代码创建一个正则表达式,并将其与“TextView”的文本进行匹配。 - Basant Singh
当我在TextView中匹配到USSD代码时,我该怎么做?我想像电话号码一样以蓝色显示它。 - Omid Nazifi
1
你可以在使用setText()方法之前检查并将PaintFlags设置为下划线,并将颜色设为蓝色。或者,你可以扩展TextView并重写onDraw()方法,在那里进行检查。 - Basant Singh
是的,也许你是对的。我应该检查一下。 - Omid Nazifi
@Shayanpourvatan,请查看下面的答案,如果它不能解决您的问题,请提出一个新的问题或者给我发送一封电子邮件。 - Omid Nazifi
显示剩余2条评论
1个回答

1

终于,我找到了解决我的请求的方法。正如@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代码的任何地方,您都应编写以下代码并调用:

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" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接