文字列にHTMLリンクを使う
HTMLリンクを使う、つまりURLリンクの作成をするには
レイアウトで Viewに
android:autoLink=””
を追加するか、
もしくは
プログラムで
setAutoLinkMask()
を使う
ほとんどの場合は、TextViewで使うことが多い
これは、シンプルだし
TextViewに準備されているメソッドとか属性が
文字の表現に特化しているから
なので、文字表示には基本的にTextViewを使う
レイアウトで設定するなら
1 2 3 4 5 6 | < TextView android:id = "@+id/text1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:autoLink = "web" |
プログラムでやるならレイアウトファイルで
@+id/text2
とかでIDをつけて、それで判別して制御する
まず
1 2 3 4 5 | < TextView android:id = "@+id/text2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "TextView" /> |
これで、パーツを用意して
あとは、Javaで制御
画面の制御とかはonCreate()の中になるので
1 2 3 | TextView txt2 = (TextView)findViewById(R.id.text2); txt2.setAutoLinkMask(Linkify.WEB_URLS); |
で設定
.setAutoLinkMask(Linkify.WEB_URLS)
でWebページを指定している
リンクの種別を変えれば
電子メールや電話番号にできる
電話番号にするなら
.setAutoLinkMask(Linkify.PHONE_NUMBERS)
にする