android_常用UI控件_01_TextView

TextView實現超鏈接和電話撥打


MainActivity.java

package com.example.android_textview_html;

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

public class MainActivity extends Activity {
    private TextView textview1,textview2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textview1 = (TextView) this.findViewById(R.id.textview1);
        textview2 = (TextView) this.findViewById(R.id.textview2);
        //設置html標誌
        String html = "<font color='red'>I love android</font><br/>";
        html+="<font color='#0000ff'><big><i>I love android</i></big></font><p>";
        html+="<big><a href='http://baidu.com'>百度</a></big>";
        html+="我的電話: + 86 010-8937483+<br/>";
        html+="我的電話: + 15995716443+<br/>";
        CharSequence charsequence = Html.fromHtml(html);
        textview1.setText(charsequence);
        textview1.setMovementMethod(LinkMovementMethod.getInstance());//點擊的時候產生超鏈接
    
        String text = "我的URL: http://sina.com\n";
        text+= "我的email: [email protected]\n";
        text+= "我的電話: + 86 010-8937483";
        textview2.setText(text);
        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;
    }

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    
    <TextView
        android:id="@+id/textview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="20sp"
        android:autoLink="all"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="20sp"
        android:autoLink="all"
        android:layout_marginTop="150sp"
        android:text="@string/hello_world" />

</RelativeLayout>

wKiom1N-cVzzSGriAAEtGx5uNFU527.jpg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章