java - textview设置属性autolink后,点击网址链接如何捕捉到点击事件或者如何让其跳转到特定的app activity中?
PHP中文网
PHP中文网 2017-04-18 09:54:06
0
8
618

1.现在有一个需求,textview中的字符串,例如:”你好,欢迎来到android世界!http://www.google.com 谷歌是我们的一个好工具“ 点击字符串中的网址不让其跳转到浏览器中,而是能够解析网址中的id,从而跳转到app中特定的activity中。我已经设置了textview的属性autolink="true"
2.目前的问题是,点击网址总是跳转到浏览器!所以我该如何捕捉到点击网址的事件或者如何跳转到指定的Activity中?

PHP中文网
PHP中文网

认证0级讲师

reply all(8)
大家讲道理

Thank you all the enthusiastic netizens for your answers. I have found the relevant answers. The details are as follows:

  1. Note android:autoLink="web"

    <TextView  
        android:id="@+id/text_view"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:autoLink="web"  
        android:text="@string/hello" />  

    2. Rewrite the click event of url

public class MainActivity extends Activity {

  
    TextView tv;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        tv = (TextView) findViewById(R.id.tv);  
        CharSequence text = tv.getText();  
        if (text instanceof Spannable) {  
            int end = text.length();  
            Spannable sp = (Spannable) text;  
            URLSpan urls[] = sp.getSpans(0, end, URLSpan.class);  
            SpannableStringBuilder style = new SpannableStringBuilder(text);  
            style.clearSpans();  
            for (URLSpan urlSpan : urls) {  
                MyURLSpan myURLSpan = new MyURLSpan(urlSpan.getURL());  
                style.setSpan(myURLSpan, sp.getSpanStart(urlSpan),  
                        sp.getSpanEnd(urlSpan),  
                        Spannable.SPAN_EXCLUSIVE_INCLUSIVE);  
  
            }  
            tv.setText(style);  
        }  
    }  
  
    private class MyURLSpan extends ClickableSpan {  
  
        private String url;  
  
        public MyURLSpan(String url) {  
            this.url = url;  
        }  
  
        @Override  
        public void onClick(View arg0) {  
            startActivity(new Intent(WXEntryActivity.this,RegisterActivity.class));  
        }  
  
    }  
  
}  

3. The above two steps perfectly solve the problem of website links being highlighted and underlined. When clicked, you can jump to the determined Activity

Finally, I would like to thank you all again for your answers, and I would also like to thank netizen @小谷xg for providing a blog reference in this regard. I have learned a lot.

阿神

Does it need to be such a roundabout way? Directly set clickable="true" on textView, then set textview.setOnclickListener click event, jump, textview.getText gets the URL

Ty80

InheritedClickableSpan重写onClickMethod

阿神

I have written this before, see if it helps

public class MainActivity extends ActionBarActivity {
    private TextView textView;
    static Context ctx = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.tv);
        ctx = this;
        textView = (TextView) findViewById(R.id.tv);
        String htmlLinkText = "<a href=\"" + "http:" + "/" + "/"
                + "www.baidu.com\"><u>我的CSDN博客 </u></a>";
        textView.setText(Html.fromHtml(htmlLinkText));
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        CharSequence text = textView.getText();
        if (text instanceof Spannable) {
            int end = text.length();
            Spannable sp = (Spannable) textView.getText();
            URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
            SpannableStringBuilder style = new SpannableStringBuilder(text);
            style.clearSpans();
            for (URLSpan url : urls) {
                MyURLSpan myURLSpan = new MyURLSpan(url.getURL());
                style.setSpan(myURLSpan, sp.getSpanStart(url),
                        sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            textView.setText(style);
        }
    }
    private static class MyURLSpan extends ClickableSpan {
        private String mUrl;
        MyURLSpan(String url) {
            mUrl = url;
        }
        @Override
        public void onClick(View widget) {
            Toast.makeText(ctx, mUrl, Toast.LENGTH_SHORT).show();
        }
    }
}
洪涛

Don’t use the autolink attribute. Directly set the click event of the textview, including a qualified String, unless there is a corresponding event: such as calling an activity

PHPzhong

Description of requirements:
1. The poster needs to underline the website link
2. The poster hopes to jump to the determined Activity when clicked

PHPzhong
    private void initAgreement(TextView text) {
        text.setClickable(true);
        text.setText(getClickableSpan(text));
        text.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private SpannableString getClickableSpan(TextView textView) {
        View.OnClickListener listener1 = new View.OnClickListener() {
            // 如下定义自己的动作
            public void onClick(View v) {
                Intent intent = new Intent(AboutActivity.this, ProtocalActivity.class);
                intent.putExtra(ProtocalActivity.TITLE_KEY, getString(R.string.user_agreement_content_href1));
                intent.putExtra(ProtocalActivity.CONTENT_URI_KEY, ProtocalActivity.END_USER_LICENSE_AGREEMENT);
                startActivity(intent);
            }
        };
        
        String agreement = textView.getText().toString();
        String h1 = getResources().getString(R.string.user_agreement_content_href1);
        int h1Index = agreement.indexOf(h1);
        SpannableString spanableInfo = new SpannableString(agreement);
        int start1 = h1Index;
        if (start1 != -1) {
            int end1 = start1 + h1.length();
            spanableInfo.setSpan(new Clickable(listener1), start1, end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spanableInfo.setSpan(new ForegroundColorSpan(Color.parseColor("#28c0c6")), start1, end1,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spanableInfo;
    }

    class Clickable extends ClickableSpan implements OnClickListener {
        private final View.OnClickListener mListener;

        public Clickable(View.OnClickListener l) {
            mListener = l;
        }

        @Override
        public void onClick(View v) {
            mListener.onClick(v);
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setUnderlineText(false);
        }
    }
Ty80

Has the group owner found a solution? I also encountered this problem

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!