如何在文本视图中单击文本链接时打开浏览器

2022-09-03 09:29:41

我做了一个有一些文字的活动。我做了一个可点击的链接,但它工作正常(链接是可见的,带有下划线)。TextView

但是当我点击链接时,它说这是我的代码。Unfortunately app has stopped responding

代码:TextView

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/home"
    android:text="@string/google" />

代码(在):Javaprotected void onCreate(Bundle savedInstanceState)

 TextView txt= (TextView) findViewById(R.id.home); //txt is object of TextView
    txt.setMovementMethod(LinkMovementMethod.getInstance());

代码:string.xml

<string name="google">
    <a href="www.google.com">Home page</a>
</string>

这是我的应用程序显示的内容,

App screenshot

现在,如果我单击“主页”链接,则会显示错误消息,指出我该怎么办?Unfortunately app has stopped not responding appears

请帮忙!


答案 1

只需在文本视图 xml 中添加以下行即可。

android:autoLink="web"

答案 2

感谢那些通过他们的回答帮助我的人

这就是完整的答案。

在写TextViewactivityname.xmlandroid:autoLink="web"

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/home"
    android:autoLink="web
    android:text="@string/google" />

在文件写入中创建javaonClickListener()

TextView txt= (TextView) findViewById(R.id.home); //txt is object of TextView
    txt.setMovementMethod(LinkMovementMethod.getInstance());
    txt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW);
            browserIntent.setData(Uri.parse("http://www.google.com"));
            startActivity(browserIntent);
        }
    });

文件中没有更改string.xml

感谢@AdnanAmjad @sarvesh @Mikejess和删除他评论的人


推荐