Android Linkify文本 - 单文本视图中的跨区文本 - 就像Twitter推文一样

MKJ*_*ekh 20 android textview linkify spannablestring

我有一个textView和文字就好

"这是带KeyWord的简单文本和浏览链接 "

在上面的文字我想做..

单击链接将打开该URL单击该KeyWord在我的应用程序中打开一个新的活动

此外, 整个TextView还有一个click事件.

请帮我找一个解决方案.谢谢你,MKJParekh

Lal*_*ani 21

你可以做的是创建一个自定义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;
    }
}
Run Code Online (Sandbox Code Playgroud)

并使用它onClick()来打开一个Activity或URL.我正在添加一个用于加载URL的演示WebView.

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());
Run Code Online (Sandbox Code Playgroud)


小智 12

以下列方式生成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());
Run Code Online (Sandbox Code Playgroud)

通过以下方式指定您的活动:

<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>
Run Code Online (Sandbox Code Playgroud)

我认为它应该有效.可能需要一些调整.我没有时间去测试它.如果有效,请告诉我.


MKJ*_*ekh 5

我使用了我在本课题中回答的相同内容

为此,我使用了这个LinkEnabledTextView,这对我来说很容易完成任务.