• 在一个Activity里面的TextView上面添加网页链接,启动后到另一个Activity里面!


    可以添加很多的属性,样式或者是什么的,目前要完成的功能是 点击TextView里面的某个文字链接,进入另外一个Activity里面!
    例如你可以做微博里面的 @XXX; 点击后进入他的个人主页!

    下面都是Activity:

    package wq.gdky005;
    
    import java.util.ArrayList;
    
    import android.R.color;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.text.Html;
    import android.text.Spannable;
    import android.text.SpannableStringBuilder;
    import android.text.method.LinkMovementMethod;
    import android.text.style.ClickableSpan;
    import android.text.style.URLSpan;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class AbcActivity extends Activity {
        private TextView tv;
        private static Context ctx;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            tv = new TextView(this);
            tv.setClickable(false);
            ctx = this;
    
            String htmlLinkText = "";
    
            htmlLinkText = "aaaaaaaaaa"
                    + "<a style="color:red;" href="我是超链接……">@超链接点击事件;</a>" +
                    "     <a style="color:red;" href="我是超链接2……">@超链接点击事件;</a>"
                    + "aaaaaaaaaaaaaaaa";
            // 文字的样式(style)被覆盖,不能改变……
    
            tv.setText(Html.fromHtml(htmlLinkText));
            tv.setMovementMethod(LinkMovementMethod.getInstance());
            CharSequence text = tv.getText();
            if (text instanceof Spannable) {
                int end = text.length();
                Spannable sp = (Spannable) tv.getText();
                URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
                SpannableStringBuilder style = new SpannableStringBuilder(text);
                style.clearSpans();// should clear old spans
                for (URLSpan url : urls) {
                    MyURLSpan myURLSpan = new MyURLSpan(url.getURL());
                    style.setSpan(myURLSpan, sp.getSpanStart(url),
                            sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                }
                tv.setText(style);
            }
            setContentView(tv);
    
        }
    
        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_LONG).show();
                widget.setBackgroundColor(Color.parseColor("#00000000"));
                
                Intent intent = new Intent(AbcActivity.ctx, AAAActivity.class);
                ctx.startActivity(intent);
                
                
            }
        }
    }

    这是设置文件的颜色和样式的

    TextView是用来显示文本的,有时需要给TextView中的个别字设置为超链接,或者设置个别字的颜色、字体等,那就需要用到Spannable对象,可以借助Spannable对象实现以上设置
    
    myTextView = (TextView) this.findViewById(R.id.myTextView);   
        
      //创建一个 SpannableString对象   
      SpannableString sp = new SpannableString("这句话中有百度超链接,有高亮显示,这样,或者这样,还有斜体.");   
      //设置超链接   
      sp.setSpan(new URLSpan("http://www.baidu.com"), 5, 7,   
      Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);   
      //设置高亮样式一   
      sp.setSpan(new BackgroundColorSpan(Color.RED), 17 ,19,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);   
      //设置高亮样式二   
      sp.setSpan(new ForegroundColorSpan(Color.YELLOW),20,24,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);   
      //设置斜体   
      sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 27, 29, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);   
      //SpannableString对象设置给TextView   
      myTextView.setText(sp);   
      //设置TextView可点击   
      myTextView.setMovementMethod(LinkMovementMethod.getInstance());   
  • 相关阅读:
    CSS学习1
    三个和尚没水喝阅读笔记
    Javascript学习1

    mv 批量
    emacs 大小写转换
    too many open files
    成都定房
    有关重定向
    postgresql 数据库
  • 原文地址:https://www.cnblogs.com/feijian/p/4269561.html
Copyright © 2020-2023  润新知