【问题标题】:How to open Google page on click on text view hyperlink in android?如何在android中点击文本视图超链接打开谷歌页面?
【发布时间】:2013-07-19 14:55:06
【问题描述】:

如何在android中点击超链接文本视图打开谷歌主页我正在尝试使用以下代码但点击超链接它只是变成普通文本如何解决它

代码如下:

MainActivity.java(在 onCreate() 中)

instruction = (TextView) findViewById(R.id.example);

    instruction.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            instruction.setText(Html.fromHtml(getString(R.string.Google_Instructions)));
            Linkify.addLinks(instruction, Linkify.ALL);
            instruction.setMovementMethod(LinkMovementMethod.getInstance());                
        }
    });

字符串.xml

<string name="Google_Instructions">opens <a href="https://www.google.co.in">Google</a> page </string>

layout_test.xml

 <TextView
        android:id="@+id/example"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10sp"
        android:clickable="true"
        android:text="@string/Google_Instructions"
        android:autoLink="web"
        android:textSize="16sp" />

【问题讨论】:

  • 你想在浏览器中打开它吗?
  • stackoverflow.com/questions/7570239/…。搜索,这样你就会有很多类似的帖子
  • 您是要在您的应用程序中嵌入网络浏览器还是在您的设备上启动默认浏览器。每个所需的代码显然差异很大
  • @MikeBaglioJr。我想在设备中启动默认浏览器

标签: android hyperlink textview


【解决方案1】:

您好,我通过在 java 类中添加一小段代码以及布局 xml 文件中的少量更改得到了解决方案

instruction = (TextView) findViewById(R.id.example);

    instruction.setText(Html
            .fromHtml("<b> To open google console </b> "
                    + "<a href=\"https://code.google.com/apis/console\">Click here</a> "));
    instruction.setMovementMethod(LinkMovementMethod.getInstance());

layout_test.xml

 <TextView
        android:id="@+id/example"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    相关资源
    最近更新 更多