Search in sources :

Example 1 with SpannableClickable

use of com.yiw.circledemo.spannable.SpannableClickable in project CircleDemo by Naoki2015.

the class PraiseListView method setClickableSpan.

@NonNull
private SpannableString setClickableSpan(String textStr, final int position) {
    SpannableString subjectSpanText = new SpannableString(textStr);
    subjectSpanText.setSpan(new SpannableClickable(itemColor) {

        @Override
        public void onClick(View widget) {
            if (onItemClickListener != null) {
                onItemClickListener.onClick(position);
            }
        }
    }, 0, subjectSpanText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return subjectSpanText;
}
Also used : SpannableString(android.text.SpannableString) TextView(android.widget.TextView) View(android.view.View) SpannableClickable(com.yiw.circledemo.spannable.SpannableClickable) NonNull(android.support.annotation.NonNull)

Example 2 with SpannableClickable

use of com.yiw.circledemo.spannable.SpannableClickable in project CircleDemo by Naoki2015.

the class UrlUtils method formatUrlString.

public static SpannableStringBuilder formatUrlString(String contentStr) {
    SpannableStringBuilder sp;
    if (!TextUtils.isEmpty(contentStr)) {
        sp = new SpannableStringBuilder(contentStr);
        try {
            //处理url匹配
            Pattern urlPattern = Pattern.compile("(http|https|ftp|svn)://([a-zA-Z0-9]+[/?.?])" + "+[a-zA-Z0-9]*\\??([a-zA-Z0-9]*=[a-zA-Z0-9]*&?)*");
            Matcher urlMatcher = urlPattern.matcher(contentStr);
            while (urlMatcher.find()) {
                final String url = urlMatcher.group();
                if (!TextUtils.isEmpty(url)) {
                    sp.setSpan(new SpannableClickable() {

                        @Override
                        public void onClick(View widget) {
                            Uri uri = Uri.parse(url);
                            Context context = widget.getContext();
                            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                            intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
                            context.startActivity(intent);
                        }
                    }, urlMatcher.start(), urlMatcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
            //处理电话匹配
            Pattern phonePattern = Pattern.compile("[1][34578][0-9]{9}");
            Matcher phoneMatcher = phonePattern.matcher(contentStr);
            while (phoneMatcher.find()) {
                final String phone = phoneMatcher.group();
                if (!TextUtils.isEmpty(phone)) {
                    sp.setSpan(new SpannableClickable() {

                        @Override
                        public void onClick(View widget) {
                            Context context = widget.getContext();
                            //用intent启动拨打电话
                            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(intent);
                        }
                    }, phoneMatcher.start(), phoneMatcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        sp = new SpannableStringBuilder();
    }
    return sp;
}
Also used : Context(android.content.Context) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Intent(android.content.Intent) View(android.view.View) Uri(android.net.Uri) SpannableStringBuilder(android.text.SpannableStringBuilder) SpannableClickable(com.yiw.circledemo.spannable.SpannableClickable)

Example 3 with SpannableClickable

use of com.yiw.circledemo.spannable.SpannableClickable in project CircleDemo by Naoki2015.

the class CommentListView method setClickableSpan.

@NonNull
private SpannableString setClickableSpan(final String textStr, final String id) {
    SpannableString subjectSpanText = new SpannableString(textStr);
    subjectSpanText.setSpan(new SpannableClickable(itemColor) {

        @Override
        public void onClick(View widget) {
            Toast.makeText(MyApplication.getContext(), textStr + " &id = " + id, Toast.LENGTH_SHORT).show();
        }
    }, 0, subjectSpanText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return subjectSpanText;
}
Also used : SpannableString(android.text.SpannableString) TextView(android.widget.TextView) View(android.view.View) SpannableClickable(com.yiw.circledemo.spannable.SpannableClickable) NonNull(android.support.annotation.NonNull)

Aggregations

View (android.view.View)3 SpannableClickable (com.yiw.circledemo.spannable.SpannableClickable)3 NonNull (android.support.annotation.NonNull)2 SpannableString (android.text.SpannableString)2 TextView (android.widget.TextView)2 Context (android.content.Context)1 Intent (android.content.Intent)1 Uri (android.net.Uri)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1