[Andriod] 以TextView實現Hyperlink

  • 5118
  • 0

...

STP1. 編輯\res\values\strings.xml如下,


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Hello</string>
    <string name="action_settings">Settings</string>
    
    <string name="textView1">http://10.243.20.82/WeeklyReport/Forms/ReportKeyin.aspx</string>
    <string name="textView2"><a href="http://10.243.20.82/WeeklyReport/Forms/ReportKeyin.aspx">QMSWeeklyReport</a></string>

</resources>

 

STP2. 編輯\res\layout\activity_main.xml如下,

@+id/textView1使用@string/textView1, android:autoLink="all"
@+id/textView2使用@string/textView2,


<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="all"
        android:text="@string/textView1" />

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

</TableLayout>

 

STP3. 編輯\src\com\example\hello\MainActivity.java如下,


package com.example.hello;

import android.os.Bundle;
import android.app.Activity;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private TextView textView2; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView2 = (TextView) findViewById(R.id.textView2);
        textView2.setMovementMethod(LinkMovementMethod.getInstance());//點擊觸發事件
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

 

STP4. 於\AndroidManifest.xml編輯網路存取連線


<uses-permission android:name="android.permission.INTERNET"/>

 

STP5. 結果如下, textView2是比較理想的結果