java 如何在单击 TextView 中的文本链接时打开浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43025993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:01:28  来源:igfitidea点击:

How do I open a browser on clicking a text link in TextView

javaandroidxmlstring.xml

提问by stodgy.nerd

I made an activity that has some text. I made a link clickable in TextViewbut and it is working fine (link is visible with underline).

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

But when I click the link it says Unfortunately app has stopped respondingHere is my code.

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

The TextViewcode:

TextView代码:

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

The Javacode (in protected void onCreate(Bundle savedInstanceState)):

Java代码(在protected void onCreate(Bundle savedInstanceState)):

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

The string.xmlcode:

string.xml代码:

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

This is what my app shows,

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

App screenshot

应用截图

Now if I click Home Pagelink the error message saying Unfortunately app has stopped not responding appearsWhat should I do?

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

Please help!

请帮忙!

回答by Adnan Amjad

Just add following line in your textview xml.

只需在您的 textview xml 中添加以下行。

android:autoLink="web"

回答by stodgy.nerd

Thanks to those who helped me by their answers

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

This is what the complete answer looks like.

这就是完整答案的样子。

In the TextViewof activityname.xmlwrite android:autoLink="web"

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" />

In the javafile write create onClickListener()

java文件中写入创建onClickListener()

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);
        }
    });

No changes in the string.xmlfile

string.xml文件中没有变化

Thanks to @AdnanAmjad @sarvesh @Mikejess and to the guy who deleted his comment

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

回答by Mike jess

text_view.setOnClickListener(new OnClickListener(){

     String url = textView.getText().toString();
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);

});

回答by sarvesh

Check this one it is working for me i have check the result both for textview and button you can use it and at the place of text you can replace the link

检查这个它对我有用 我已经检查了 textview 和按钮的结果,您可以使用它,并且在文本位置您可以替换链接

            <Button
                android:layout_width="fill_parent"
                android:layout_height="20dp"
                android:text=" Click To Send Email "
                android:textColor="#FFF"
                android:clickable="true"
                android:background="#cf1414"
                android:linksClickable="true"
                android:id="@+id/emailbutton"/>