最終更新:
moonlight_aska 2012年04月15日(日) 22:42:21履歴
URLやメールアドレスなどの文字列をリンクに変換する方法は2通りある.
には, テキストビュー(TextView)クラスのsetAutoLinkMaskメソッドをしようする.

には, テキストビュー(TextView)クラスのsetAutoLinkMaskメソッドをしようする.

- res/values/strings.xml
- リンクに変換する文字列を定義する.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">文字列をリンクに変換する</string>
<string name="text_label">Link : http://www.yahoo.co.jp</string>
</resources>
- res/layout/main.xml
- "android:autoLink"属性に, 文字列の何をリンクに変換するか設定する.
引数 | 対象 |
all | 下記すべて |
emal | メールアドレス |
map | マップアドレス |
phone | 電話番号 |
web | URL |
- "@string/text_label"で表示する文字列を指定する.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_id"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/textview_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:text="@string/text_label"
/>
</LinearLayout>
- TextView01.java
- findViewByIdメソッドで, TextViewのインスタンスを取得する.
- TextView#setAutoLinkMaskメソッドで, 文字列の何をリンクに変換するか設定する.
引数 | 対象 |
Linkify.ALL | 下記すべて |
Linkify.EMAIL_ADDRESS | メールアドレス |
Linkify.MAP_ADDRESS | マップアドレス |
Linkify.PHONE_NUMBERS | 電話番号 |
Linkify.WEB_URLS | URL |
- TextView#setTextメソッドで, 変換する文字列を設定する.
package com.moonlight_aska.android.textview01;
import android.app.Activity;
import android.os.Bundle;
import android.text.util.Linkify;
import android.widget.TextView;
public class TextView01 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text = (TextView)findViewById(R.id.textview_id);
// リンクに変換するものをMask値で決める.
text.setAutoLinkMask(Linkify.ALL);
text.setText("Link : http://www.yahoo.co.jp");
}
}
コメントをかく