2011-09-27 31 views
16

我有一個TextView和文字一樣的Android Linkify文本 - 在單文本視圖Spannable文字 - 像Twitter的鳴叫

「這是關鍵字的簡單文本和鏈接瀏覽」

在上面的文字我想使..

點擊鏈接去打開URL點擊該關鍵字Ø在我的應用程序中筆新活動

甚至有整個TextView的點擊事件。

請幫我找到解決方案。 謝謝, MKJParekh

+0

我想你應該檢查:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/text/Link.html http://developer.android.com/資源/文章/ wikinotes-linkify.html –

+2

嗯,我想這就是你要找的[Android自定義超鏈接TextView](http://webcache.googleusercontent.com/search?q=cache:http://www。 orangeapple.org/) –

+1

@Lalit Poptani它幫助我感謝你 – user609239

回答

-2

使關鍵字和鏈接到不同的TextViews。

然後在完成後設置TextView.onClickListener()。

這是最簡單的方式,更多的邏輯方式。

+0

是的,我知道我可以這樣做......但只需要一個文本視圖 – MKJParekh

11

生成以下方式你的TextView:

TextView t3 = (TextView) findViewById(R.id.text3); 
t3.setText(Html.fromHtml("This is the Simple Text with 
    <a href=\'http://www.google.com\'>Keyword</a> and the 
    <a href='startActivityFromLink://some_info'>Link</a> to browse")); 
t3.setMovementMethod(LinkMovementMethod.getInstance()); 

指定的活動方式如下:

<activity android:name=".TestActivity" 
     android:label="@string/app_name"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
    <intent-filter> 
     <data android:scheme="startActivityFromLink" /> 
     <action android:name="android.intent.action.VIEW" /> 
     <category android:name="android.intent.category.BROWSABLE" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</activity> 

,我認爲它應該工作。可能需要一些調整。我沒有時間去測試它。讓我知道它是否有效。

19

你可以做的是創建一個自定義ClickableSpan如下,

class MyCustomSpannable extends ClickableSpan 
{ 
    String Url; 
    public MyCustomSpannable(String Url) { 
     this.Url = Url; 
    } 
    @Override 
    public void updateDrawState(TextPaint ds) { 
      // Customize your Text Look if required 
     ds.setColor(Color.YELLOW); 
     ds.setFakeBoldText(true); 
     ds.setStrikeThruText(true); 
     ds.setTypeface(Typeface.SERIF); 
     ds.setUnderlineText(true); 
     ds.setShadowLayer(10, 1, 1, Color.WHITE); 
     ds.setTextSize(15); 
    } 
    @Override 
    public void onClick(View widget) { 
    } 
    public String getUrl() { 
     return Url; 
    } 
} 

而且利用其onClick()打開一個活動或URL。我在WebView中添加了一個加載URL的演示。

String text = "http://www.google.co.in/"; 
SpannableStringBuilder stringBuilder = new SpannableStringBuilder(text); 
MyCustomSpannable customSpannable = new MyCustomSpannable(
               "http://www.google.co.in/"){ 

      @Override 
      public void onClick(View widget) { 
       Intent intent = new Intent(Intent.ACTION_VIEW); 
       intent.setData(Uri.parse(customSpannable.getUrl())); 
       startActivity(intent); 
      } 
     }; 
     stringBuilder.setSpan(customSpannable, 0, text.length(), 
             Spannable.SPAN_INCLUSIVE_INCLUSIVE); 

     textView.setText(stringBuilder, BufferType.SPANNABLE); 
     textView.setMovementMethod(LinkMovementMethod.getInstance()); 
+0

感謝您的答案,它幫助了我很多。 – MKJParekh