2013-09-02 34 views
5

Tôi có liên kết trong TextView tôi:Tại sao liên kết không hoạt động trong chế độ xem văn bản?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    ... 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:autoLink="all" 
     android:linksClickable="true" 
     android:text="@string/app_description" /> 
</RelativeLayout> 

Liên kết là một phần của tài nguyên:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    ... 
    <string name="app_description">Some text with the <a href="http://www.example.com">link</a>.</string> 
</resources> 

Bằng cách nào đó liên kết không hoạt động. Điều gì có thể là một lý do? Nó được hiển thị giống như một liên kết. Nhưng nhấp chuột không mở trình duyệt.

Mã lớp:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
+0

Bạn có thử với android: nhấp được = "true"? –

+0

Xem liệu [Làm cách nào để tạo liên kết trong TextView có thể nhấp] (http://stackoverflow.com/questions/2734270/how-do-i-make-links-in-a-textview-clickable) hoặc [Liên kết trong TextView ] (http://stackoverflow.com/questions/4790746/links-in-textview) câu hỏi sẽ giúp ích. –

+0

Hãy thử cách này: android: autoLink = "web" –

Trả lời

6

bạn có thể sử dụng liên kết http theo cách này:

mLink = (TextView) findViewById(R.id.link); 
     if (mLink != null) { 
     mLink.setMovementMethod(LinkMovementMethod.getInstance()); 
    } 

Trong XML:

android:autoLink="all" 
+0

Cảm ơn. Câu trả lời của bạn cùng với http://stackoverflow.com/a/6892799/604388 đã giúp ích. –

+0

Không hoạt động với phông chữ tùy chỉnh. Bất kỳ đầu mối? – ADM

1

TextView yourTextView = (TextView) findViewById (R.id .yourTextViewId);

yourTextView.setMovementMethod(LinkMovementMethod.getInstance()); 
    Spannable spans = (Spannable) yourTextView.getText(); 

    ClickableSpan clickSpan = new ClickableSpan() { 

     @Override 
     public void onClick(View v) { 
      switch (v.getId()) { 

      case R.id.yourTextViewId : 
       Intent localIntent = new Intent(); 
       localIntent.setAction("android.intent.action.VIEW"); 
       localIntent.setData(Uri.parse("YOUR LINK")); 
       startActivity(localIntent); 
       break; 
      } 

     } 
    }; 
    spans.setSpan(clickSpan, 0, spans.length(), 
      Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
0

enter image description here

mã sau đây đang làm việc cho tôi.

String html2 = "<br><br>Fire - <b><a href=http://www.google.com>google</a> </b></br></br>"; 
     text.append(Html.fromHtml(html2)); 
     text.setMovementMethod(LinkMovementMethod.getInstance()); 
0

Xóa android: autoLink từ định nghĩa bố cục. Đó là quá nhiều trong trường hợp nếu trường hợp của bạn. Bọc chuỗi tài nguyên với <![CDATA[ ]]> như thế này:

<?xml version="1.0" encoding="utf-8"?> <resources> ... <string name="app_description"><![CDATA[Some text with the <a href="http://www.example.com">link</a>.]]></string> </resources>

+1

Có vẻ như câu trả lời của bạn đã bị cắt - bạn có thể chỉnh sửa câu trả lời để hoàn thành câu trả lời không? –

Các vấn đề liên quan