是否Linkify Android中工作的TextView? [英] Does Linkify work for TextView in Android?

查看:192
本文介绍了是否Linkify Android中工作的TextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个code工作为我的方法调用一个EditText,我试图用同样的$ C $下一个TextView,但它不工作。文本不会变成一个超链接像它在EditText上,没有任何人知道为什么吗?

I have this code working for my method that calls an EditText, I tried to use the same code for a TextView but it does not work. The text does not turn into a hyperlink like it does in EditText, does anybody know why?

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同样的事情

This will work fine in EditText, i just need help doing the same thing in TextView

推荐答案

有可点击的范围和设定与clikable跨距中的文本。您可以自定义颜色的clickabke跨度。当你点击在TextView中会显示一个敬酒的文本。

Have clickable span and set the text with the clikable span. You can have custom color for the clickabke span. When you click on the text in textview it displays a 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

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

所得的快拍

编辑:

打开与点击的TextView的文本链接的浏览器。您还可以通过链接直接一个活动。检索URL和加载的URL的web视图。

Open a browser with the url on click on text in textview. You can also pass the url to a activity. Retrieve the url and load the url in webview.

 <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()

In onClick()

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

second.xml

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

Then in SecondActivty

公共类SecondActivity延伸活动{

public class SecondActivity extends 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"));
    }   
} 
 }

这篇关于是否Linkify Android中工作的TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆