Android TextView添加超链接的方法示例
本文实例讲述了AndroidTextView添加超链接的方法。分享给大家供大家参考,具体如下:
publicclassLinkextendsActivity{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.link); //text1showstheandroid:autoLinkproperty,which //automaticallylinkifiesthingslikeURLsandphonenumbers //foundinthetext.Nojavacodeisneededtomakethis //work. //text2haslinksspecifiedbyputting<a>tagsinthestring //resource.Bydefaulttheselinkswillappearbutnot //respondtouserinput.Tomakethemactive,youneedto //callsetMovementMethod()ontheTextViewobject. TextViewt2=(TextView)findViewById(R.id.text2); t2.setMovementMethod(LinkMovementMethod.getInstance()); //text3showscreatingtextwithlinksfromHTMLintheJava //code,ratherthanfromastringresource.Notethatfora //fixedstring,usinga(localizable)resourceasshownabove //isusuallyabetterwaytogo;thisexampleisintendedto //illustratehowyoumightdisplaytextthatcamefroma //dynamicsource(eg,thenetwork). TextViewt3=(TextView)findViewById(R.id.text3); t3.setText( Html.fromHtml( "<b>text3:</b>Textwitha"+ "<ahref=\"http://www.google.com\">link</a>"+ "createdintheJavasourcecodeusingHTML.")); t3.setMovementMethod(LinkMovementMethod.getInstance()); //text4illustratesconstructingastyledstringcontaininga //linkwithoutusingHTMLatall.Again,forafixedstring //youshouldprobablybeusingastringresource,nota //hardcodedvalue. SpannableStringss=newSpannableString( "text4:Clickheretodialthephone."); ss.setSpan(newStyleSpan(Typeface.BOLD),0,6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss.setSpan(newURLSpan("tel:4155551212"),13,17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); TextViewt4=(TextView)findViewById(R.id.text4); t4.setText(ss); t4.setMovementMethod(LinkMovementMethod.getInstance()); } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android视图View技巧总结》、《Android布局layout技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。