Linkify是否适用于Android中的TextView?

时间:2013-04-17 14:26:22

标签: java android linkify

我有这个代码用于调用EditText的方法,我尝试使用相同的代码用于TextView,但它不起作用。文本不会像在EditText中那样变成超链接,有人知道为什么吗?

public class MainActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView tv = (TextView) findViewById(R.id.link_view);
    // make sure that setText call comes BEFORE Linkify.addLinks call
    tv.setText(tv.getText().toString());
    Linkify.addLinks(tv, Linkify.WEB_URLS);
}}

这是布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TableRow>

        <TextView
            android:id="@+id/link_lbl"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:paddingRight="10dip"
            android:text="Link" />

        <TextView
            android:id="@+id/link_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="google.com" />
    </TableRow>
</TableLayout>

这在EditText中可以正常工作,我只需要帮助在TextView中做同样的事情

2 个答案:

答案 0 :(得分:2)

试试以下代码。它对我来说很好。

TextView tv = ....
tv.setMovementMethod(LinkMovementMethod.getInstance());

String content = tv.getText().toString();
List<String> links = new ArrayList<String>();

Pattern p = Patterns.WEB_URL;
Matcher m = p.matcher(content);
while (m.find()) {
    String urlStr = m.group();
    links.add(urlStr);
}

SpannableString f = new SpannableString(content);

for (int i = 0; i < links.size(); i++) {
    final String url = links.get(i);

    f.setSpan(new InternalURLSpan(new OnClickListener() {
        public void onClick(View v) {
            Context ctx = v.getContext();
            String urlToOpen = url;
            if (!urlToOpen.startsWith("http://") || !urlToOpen.startsWith("https://"))
                urlToOpen = "http://" + urlToOpen;
            openURLInBrowser(urlToOpen, ctx);
        }
    }), content.indexOf(url), content.indexOf(url) + url.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

tv.setText(f);

答案 1 :(得分:2)

具有可点击范围并使用可缓冲范围设置文本。您可以为clickabke范围设置自定义颜色。当您单击textview中的文本时,它会显示一个Toast。

String title="hello";
SpannableString ss1=  new SpannableString(title);
    ss1.setSpan(new MyClickableSpan(title), 0, ss1.length(), 0); 
    tv = (TextView) findViewById(R.id.textview);
    tv.setText(ss1);
    tv.setMovementMethod(LinkMovementMethod.getInstance());  

MyClickableSpan

   class MyClickableSpan extends ClickableSpan{     
   String clicked;
   public MyClickableSpan(String string)  
   {
    super();
    clicked =string;
   }
   public void onClick(View tv) 
   {
     // onclick of text in textview do something 
 Toast.makeText(MainActivity.this,clicked ,Toast.LENGTH_SHORT).show();
     //display a toast 
   }
   public void updateDrawState(TextPaint ds)
   {
     ds.setColor(Color.BLUE);//set text color 
     ds.setUnderlineText(true); // set to false to remove underline
   }
  } 

产生的快照

enter image description here

编辑:

点击textview中的文字,打开带有网址的浏览器。您还可以将URL传递给活动。检索网址并在网页视图中加载网址。

 <uses-permission android:name="android.permission.INTERNET"/>

public void onClick(View tv) {
//do something

   Toast.makeText(MainActivity.this,clicked ,
        Toast.LENGTH_SHORT).show();
   String url = "http://www.example.com";
   Intent i = new Intent(Intent.ACTION_VIEW);
   i.setData(Uri.parse(url));
   startActivity(i);
}


                OR

在onClick()

   Intent t= new Intent(MainActivity.this,SecondActivity.class);
   t.putExtra("key","http://www.google.com");
   startActivity(t);

second.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="vertical" >

  <WebView
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/wv"></WebView>
  </LinearLayout>

然后在SecondActivty

公共类SecondActivity扩展了Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    WebView wv= (WebView) findViewById(R.id.wv);
    Bundle extras= getIntent().getExtras();
    if(extras!=null)
    {
        wv.loadUrl(extras.getString("key"));
    }   
} 
 }