如何讓TextView顯示出超連結

如何讓TextView顯示出超連結

情境

如果想讓TextView上面的文字能跟自動判斷是否有 Email、電話或者網址
並且讓它自動標示為超連結, 應該怎麼做?
其實, TextView早就幫你做好了!
因此只需要幾行文字, 就可以讓你完成這個簡單的功能。

程式碼說明

首先我們先設定三個元件, 分別是TextView、EditText跟Button。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.givemepass.autolinkdemo.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text_view"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/input_text"
        android:id="@+id/edit_text" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/button_text"
        android:id="@+id/send_button" />
</LinearLayout>

然後只需要在Activity內寫入一個事件, 就可以完成這次簡單的功能了。

public class MainActivity extends AppCompatActivity {
    private Button sendBtn;
    private TextView textView;
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendBtn = (Button)findViewById(R.id.send_button);
        textView = (TextView)findViewById(R.id.text_view);
        editText = (EditText)findViewById(R.id.edit_text);
        textView.setAutoLinkMask(Linkify.ALL);
        sendBtn.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                textView.setText(editText.getText().toString());
            }
        });

    }
}

我們的重點放在TextView的內建屬性, 根據官網顯示

public final void setAutoLinkMask (int mask)

Added in API level 1
Sets the autolink mask of the text. See Linkify.ALL and peers for possible values.
Related XML Attributes
android:autoLink

官方網站的TextView可以使用AutoLink這個屬性, 他會將TextView內的文字自動轉換成超連結。
利用Linkify這個類別的參數來設定mask。

int ALL Bit mask indicating that all available patterns should be matched in methods that take an options mask
int EMAIL_ADDRESSES Bit field indicating that email addresses should be matched in methods that take an options mask
int MAP_ADDRESSES Bit field indicating that street addresses should be matched in methods that take an options mask
int PHONE_NUMBERS Bit field indicating that phone numbers should be matched in methods that take an options mask
int WEB_URLS Bit field indicating that web URLs should be matched in methods that take an options mask

選擇ALL, 代表可以將以上四種功能都抓到。
一般文字


網址


信箱