Android中的Linkify TextView是什麼?
在進入示例之前,我們應該瞭解什麼是linkify。Linkify就像HTML中的超連結一樣。使用它,我們可以瀏覽內容。以下是Android中使用linkify與textview的簡單解決方案。
步驟1 - 在Android Studio中建立一個新專案,轉到檔案⇒新建專案,並填寫所有必要的細節以建立一個新專案。
步驟2 - 將以下程式碼新增到res/layout/activity_main.xml。
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Result Data" android:textSize="20sp" android:padding="10dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
在上面的XML中,我們提供了一個textview,textview包含文字和網路url連結。
步驟3 - 將以下程式碼新增到src/MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.util.Linkify;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.result);
textView.setText("TutorialsPoint.com originated from the idea that there exists a class of
readers who respond better to online content and prefer to learn new skills at their ow...");
Linkify.addLinks(textView, Linkify.WEB_URLS);
}
}在上面的程式碼中,我們給出了文字檢視並添加了一些文字。在該文字中,我們給出了一個url為tutorialspoint.com。要在Android中呼叫linkify,我們必須呼叫linkify.addLinks(),在這個方法中,我們必須傳遞textview和LinkifyMask。
如下所示,有不同型別的LinkifyMask可用 -
Linkify.WEB_URLS:它將URL設為web url,當用戶點選它時,它會將url傳送到預設的web瀏覽器。
Linkify.EMAIL_ADDRESSES:它將電子郵件ID設為linkify電子郵件ID,當用戶點選它時,它將開啟手機上的預設電子郵件客戶端。
Linkify.PHONE_NUMBERS:它將電話號碼設為linkify電話號碼,當用戶點選它時,它會將電話號碼傳送到預設撥號器。
Linkify.ALL:它將處理Linkify.WEB_URLS、Linkify.EMAIL_ADDRESSES和Linkify.PHONE_NUMBERS。
步驟4 - 將以下程式碼新增到manifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.andy.myapplication"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
讓我們嘗試執行您的應用程式。我假設您已將您的實際Android移動裝置連線到您的計算機。要從Android Studio執行應用程式,請開啟您的一個專案活動檔案,然後單擊執行
工具欄中的圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 -

在上面的輸出中,當您點選linkify文字時,它將在web瀏覽器中顯示網站,如下所示 -

在上面的輸出中,我們點選了連結。它在預設的web瀏覽器中打開了網站,如下所示 -

點選此處下載專案程式碼
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP