textView 하이퍼링크 추가(두 가지 구현 방식)

textView에 하이퍼링크를 추가하는 방법은 두 가지가 있습니다. 첫 번째는 HTML을 통해 웹 주소를 포맷하는 것입니다. 하나는 autolink를 설정하여 시스템이 자동으로 하이퍼링크를 식별하도록 하는 것입니다.코드는 다음과 같다. 첫 번째
 
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
TextView textView = new TextView(this);
String html = " :
";
html+="<a href='http://www.baidu.com'> </a>";// , http://。

// , activity, activity , 。
CharSequence charSequence = Html.fromHtml(html);

textView.setText(charSequence);

textView.setMovementMethod(LinkMovementMethod.getInstance());
layout.addView(textView);
this.setContentView(layout,params);
}
두 번째
 
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
TextView textView = new TextView(this);
String html = " :
";
html+="www.baidu.com";// HTTP; 。
textView.setText(html);
textView.setAutoLinkMask(Linkify.ALL);
textView.setMovementMethod(LinkMovementMethod.getInstance());
layout.addView(textView);
this.setContentView(layout,params);
}
요약하면 html로 하이퍼링크를 표시하려면 반드시 전체 URL을 써야 한다.setAutoLinkMask(Linkify.ALL)로 다 쓰지 않아도 자동으로 식별할 수 있다.이 두 가지 방법은 모두 set Movement Method를 설정해야만 점프할 수 있다.또한 setAutoLinkMask는 하이퍼링크를 식별할 뿐만 아니라 전화번호 등도 포함한다.

좋은 웹페이지 즐겨찾기