2014-04-08 144 views
0

我試圖在Android中創建可點擊的文本瀏覽器,但沒有成功,在佈局android中我有正確的輸出但不能鏈接。Android中的可點擊TextView

這就是我現在所擁有的:

<TextView 
    android:id="@+id/textTitle" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:autoLink="web" 
    android:clickable="true" 
    android:textSize="16sp" 
    android:textStyle="bold" /> 


     textTitle.setText(Html.fromHtml(response.toString())); 
     textTitle.setMovementMethod(LinkMovementMethod.getInstance()); 

Log.i("myApp1", response.toString()); 

我已經

<a href=http://...>MyLink</a> 
+0

問題是什麼?什麼錯誤出現,如果有的話? –

+0

我在佈局android中沒有錯誤...我有文本標題不可點擊... – comex

+0

@comex您點擊時想要什麼?改變其風格? –

回答

1

你只應該監聽器註冊到這個TextView

findViewbyId(R.id.textTitle).setOnClickListener(new OnClickListener{ 
    @Override 
    protected void onClick(View view){ 
     String link = (TextView)view.getText().toString(); 
     /* redirect to URL here for example: */ 
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link)); 
     startActivity(intent); 
    } 
}); 

或者:

添加android:autoLink="all"屬性爲您TextView並將其文本設置一個HTML元素A。例如:

<TextView 
    android:text="@string/mylink" 
    android:autoLink="all"/> 

而且在strings.xml

<string name="mylink"><a href='http://www.google.com'>http://www.google.com</a></string> 
0

XML的觀點是絕對正確的,請確保您已經務實地處理了onClickListener正常。

+0

在XML佈局中不可能在textView中使用html'MyLink'? – comex

+0

@comex是的,這是可能的,請我的回答 –

+0

@ABFORCE我答覆你 – comex