2013-04-17 32 views
3

我有這個代碼爲我的方法調用一個EditText,我試圖使用相同的代碼爲TextView,但它不起作用。文本不像EditText那樣變成超鏈接,有人知道爲什麼嗎?Linkify適用於Android中的TextView嗎?

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

+0

http://developer.android.com/reference/android/text/util/Linkify.html – Raghunandan

回答

2

有可點擊的範圍,並設置具有可克服範圍的文本。您可以爲clickabke範圍提供自定義顏色。當你點擊textview中的文本時,它會顯示一個敬酒。

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的文本鏈接的瀏覽器。您也可以將網址傳遞給某個活動。檢索網址並在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()

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延伸活動{

@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")); 
    } 
} 
} 
+0

如果我把網站它加載它? – Lucas

+0

@lucas你需要在onClick()中處理。有一個webview並加載onClick()中的URL或啓動一個新的活動,然後加載一個帶有URL的webview – Raghunandan

+0

@Lucas檢查編輯 – Raghunandan

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); 
+0

Vmerror我無法實現你的代碼,你能幫我嗎?我是一個初學者與Android – Lucas