Android Linkify 示例含代碼

Android Linkify 示例

Linkify 類用于從 TextView 或 Spannable 中創建鏈接。愛掏網 - it200.com它根據文本值和正則表達式的匹配模式,將文本和正則表達式轉換為可點擊的鏈接。愛掏網 - it200.com Linkify 類通過使用模式來創建網頁 URL、電子郵件地址、電話號碼和地圖地址的鏈接。愛掏網 - it200.com

Android 可點擊的鏈接可以通過兩種不同的方式創建:

1. 使用布局 (.xml) 文件:使用指定類型的 autoLink 屬性。愛掏網 - it200.com

<TextView
     android:id="@+id/url"
            android:autoLink="web"/>

2. 使用Java類:它使用了指定類型的Linkify類的addlLinks()方法。愛掏網 - it200.com

TextView webURL = new TextView(this);
webURL.setText("https://www.yoururl1.com/");
Linkify.addLinks(webURL , Linkify.WEB_URLS);

在該示例中,我們將為網頁URL、電子郵件地址和電話號碼創建鏈接。愛掏網 - it200.com在布局目錄中創建一個 activity_main.xml 文件,并添加以下代碼。愛掏網 - it200.com

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="example.javatpoint.com.androidlinkify.MainActivity">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp">
    </ListView>
</RelativeLayout>

創建一個自定義布局 myList.xml 文件并添加以下代碼。愛掏網 - it200.com在此布局中,我們使用 autoLink 屬性與 web、email 和 phone 屬性。愛掏網 - it200.com

myList.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:padding="5dp" />

    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/url"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:autoLink="web"
            android:textStyle="bold"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:padding="2dp"
            android:textColor="#4d4d4d" />
        <TextView
            android:id="@+id/email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:autoLink="email"
            android:layout_marginLeft="10dp"/>
        <TextView
            android:id="@+id/phonenumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:autoLink="phone"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>
</LinearLayout>

創建一個適配器類 MyListActivity.java 并使用以下代碼擴展 ArrayAdapter<>。愛掏網 - it200.com

MyListActivity.java

package example.javatpoint.com.androidlinkify;

import android.app.Activity;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyListAdapter extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] url;
    private final String[] email;
    private final String[] phonenumber;
    private final Integer[] imgid;

    public MyListAdapter(Activity context, String[] url,String[] email,String[] phonenumber, Integer[] imgid) {
        super(context, R.layout.mylist, url);
        // TODO Auto-generated constructor stub
        this.context=context;
        this.url=url;
        this.email=email;
        this.phonenumber=phonenumber;
        this.imgid=imgid;

    }

    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.mylist, null,true);

        TextView titleText = (TextView) rowView.findViewById(R.id.url);
        TextView subtitleText = (TextView) rowView.findViewById(R.id.email);
        TextView phonenumberText = (TextView) rowView.findViewById(R.id.phonenumber);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        imageView.setImageResource(imgid[position]);

        titleText.setText(url[position]);
        subtitleText.setText(email[position]);
        phonenumberText.setText(phonenumber[position]);
        Linkify.addLinks(phonenumberText,Linkify.PHONE_NUMBERS);

        return rowView;
    };

}

最后,在 MainActivity.java 類中,將適配器實例設置給ListView。愛掏網 - it200.com

MianActivity.java

package example.javatpoint.com.androidlinkify;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {
    ListView list;

    String[] urls ={
            "https://www.yoururl1.com/","https://www.yoururl2.com/",
            "https://www.yoururl3.com/","https://www.yoururl4.com/",
            "https://www.yoururl5.com/","https://www.yoururl6.com/",
            "https://www.yoururl7.com/","https://www.yoururl8.com/",
            "https://www.yoururl9.com/","https://www.yoururl19.com/",
    };

    String[] emails ={
            "email_1@mail.com","email_2@mail.com",
            "email_3@mail.com","email_4@gmail.com",
            "email_5@mail.com","email_6@mail.com",
            "email_7@mail.com","email_8@mail.com",
            "email_9@gmail.com","email_10@mail.com"
    };

    String[] phoneNumber ={
            "+(1234)-567","+(1234)-567",
            "7563654321","9475000000","8575000000",
            "+1 (919) 555-1212","+91 (919) 555-1212",
            "6463654321","5475000000","8575000000",
    };

    Integer[] imgid={
            R.drawable.image_1,R.drawable.image_2,
            R.drawable.image_3,R.drawable.image_4,
            R.drawable.image_5, R.drawable.image_6,
            R.drawable.image_7,R.drawable.image_8,
            R.drawable.image_9,R.drawable.image_10,
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyListAdapter adapter=new MyListAdapter(this, urls, emails,phoneNumber,imgid);
        list= findViewById(R.id.list);
        list.setAdapter(adapter);
    }
}

輸出:

聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 精品国产va久久久久久久冰| 一本一本久久a久久综合精品蜜桃| jizzjizz丝袜老师| 欧美黄色一级在线| 毛茸茸bbw亚洲人| 教官你的太大了芊芊h| 国产悠悠视频在线播放| 五月天婷婷久久| 国产亚洲成归v人片在线观看| 橘子没熟svk| 国产日韩一区二区三区在线播放 | 毛片在线播放网址| 在线国产中文字幕| 亚洲欧美日韩高清一区二区三区| 97精品人妻一区二区三区香蕉| 永久在线免费观看| 国产精品美女久久久免费| 亚洲成a人片在线观看www| 中国好声音第二季免费播放| 久久综合丝袜长腿丝袜| 最近免费中文字幕大全视频 | 免费A级毛片无码无遮挡| chinese激烈高潮HD| 浮力影院第一页小视频国产在线观看免费| 多人伦交性欧美在线观看| 国产一区二区三区不卡av| 亚洲国产一区二区三区在线观看 | 日韩一品在线播放视频一品免费| 国产又粗又猛又黄又爽无遮挡| 久久久久人妻精品一区蜜桃| 羞羞色在线观看| 日韩一区二区三区精品| 国产一卡二卡≡卡四卡免费乱码| 中文天堂最新版www官网在线| 福利网站在线播放| 在线精品国精品国产不卡| 免费看少妇作爱视频| 99国产欧美久久精品| 男女下面一进一出免费无遮挡| 国产香蕉尹人在线观看视频| 亚洲av无码一区二区三区观看 |