2016-02-28 16 views
0

多個環節我在數據庫中的字符串,它是這樣的:的onclick坐落於單一的TextView

string f = "this is the <a href="/page1"> first link</a> and this is the <a href="/page1"> second link</a>" 
    textview1.TextFormatted = Html.FromHtml(f); 
    url =? 
    Intent i = new Intent(Android.Content.Intent.ActionView,url); 
    StartActivity(i); 

的字符串中的鏈接數量是不同的。我想讓textview中的所有鏈接都是可點擊的,當用戶點擊其中的每個鏈接時,該鏈接的網址會發送到另一個活動。

回答

0

當你使用Html.fromHtml設置文本時,''在textView中被替換爲UrlSpans。 您可以獲取每個url跨度併爲onClick函數設置可點擊的跨度。

有關解決方案代碼,請參閱this

0

我已經實現了相同的使用SpannableStringBuilder

簡單的初始化要添加2個或更多的聽衆,然後TextView的傳遞到我已創建了以下方法:

private void customTextView(TextView view) { 
      SpannableStringBuilder spanTxt = new SpannableStringBuilder(
        getString(R.string.read_and_accept)); 
      spanTxt.setSpan(new ForegroundColorSpan(ContextCompat.getColor(activity, R.color.black_30)), 0, 
        getString(R.string.read_and_accept).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
      spanTxt.append(" "); 
      spanTxt.append(getString(R.string.t_and_c)); 
      spanTxt.setSpan(new ClickableSpan() { 
       @Override 
       public void onClick(View widget) { 
        Utils.redirectUserToUrl(activity,"http://luxit-driver-terms.tookan.in"); 
       } 
      }, spanTxt.length() - getString(R.string.t_and_c).length(), spanTxt.length(), 0); 
      spanTxt.append(" and "); 
      spanTxt.setSpan(new ForegroundColorSpan(ContextCompat.getColor(activity, R.color.accent)), 48, spanTxt.length(), 0); 
      spanTxt.append(getString(R.string.privacy_policy)); 
      spanTxt.setSpan(new ClickableSpan() { 
       @Override 
       public void onClick(View widget) { 
        Utils.redirectUserToUrl(activity,"http://luxit-driver-privacypolicy.tookan.in/"); 
       } 
      }, spanTxt.length() - getString(R.string.privacy_policy).length(), spanTxt.length(), 0); 
      view.setMovementMethod(LinkMovementMethod.getInstance()); 
      view.setText(spanTxt, TextView.BufferType.SPANNABLE); 
     } 

在XML中,使用Android:textColorLink添加自定義顏色UR鏈接文本

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="text" 
    android:textColorLink="#000000" />