如何在Android上单击文本视图超链接打开Goog​​le页面?

时间:2013-07-19 14:55:06

标签: android hyperlink textview

  

如何在Android上点击超链接文本视图打开Goog​​le主页我正在尝试使用以下代码,但点击超链接它只是更改为普通文本如何解决它

以下是代码:

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

的strings.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" />

1 个答案:

答案 0 :(得分: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" />