//鏈接文字相關 tv_phone=(TextView) findViewById(R.id.tv_lineschedule_phone); tv_neturl=(TextView) findViewById(R.id.tv_lineschedule_neturl); //創建一個 SpannableString對象 SpannableString sp_phone = new SpannableString("123456789");//顯示內容 SpannableString sp_neturl = new SpannableString("www.dianping.com"); //設置超鏈接 內容 sp_phone.setSpan(new URLSpan("tel:123456789"), 0, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); sp_neturl.setSpan(new URLSpan("http://www.google.com"), 0, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //設置高亮樣式一 sp_phone.setSpan(null, 0 ,9,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); sp_neturl.setSpan(null, 0 ,16,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //SpannableString對象設置給TextView tv_phone.setText(sp_phone); tv_neturl.setText(sp_neturl); //設置TextView可點擊 tv_phone.setMovementMethod(LinkMovementMethod.getInstance()); tv_neturl.setMovementMethod(LinkMovementMethod.getInstance());
方法二:
tv01 = (TextView)findViewById(R.id.tv01);
tv01.setTextColor(Color.RED);
tv01.setTextSize(22f);
//html字符:超鏈接
String htmlhref = "<a href=\"http:www.google.com.h快\">百度</a>";
//Html.fromHtml解析HTML,生成String
tv01.setText(Html.fromHtml(htmlhref));
//html字符:只顯示超練級,不使用
//tv01.setAutoLinkMask(Linkify.All);
//出來顯示還可以點擊
tv01.setMovementMethod(LinkMovementMethod.getInstance()