📜  Android Linkify示例

📅  最后修改于: 2020-10-11 05:29:52             🧑  作者: Mango

Android Linkify示例

Linkify类用于从TextView或Spannable创建链接。它将根据文本值和正则表达式的模式匹配将文本和正则表达式转换为可单击的链接。 Linkify类使用模式创建Web URL,电子邮件地址,电话号码和地图地址的链接。

Android可点击链接可以通过两种不同方式创建:

1.使用布局(.xml)文件:它使用具有指定类型的autoLink属性。


2.使用Java类:它对指定类型的Linkify类使用addlLinks()方法。

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

Android Linkify示例

在此示例中,我们将为Web URL,电子邮件地址和电话号码创建链接。在布局目录中创建一个activity_main.xml文件,并添加以下代码。

activity_main.xml




    
    

创建一个自定义布局myList.xml文件,并添加以下代码。在此布局中,我们将autoLink属性与web,email和phone属性一起使用。

myList.xml




    

    

        
        
        
    

创建一个适配器类MyListActivity.java并使用以下代码扩展ArrayAdapter <>。

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 {

    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。

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);
    }
}

输出: