TextView超链接输出

android中的TextView类似于ASP.NET的中的Label,HTML中的<label></lable>。

如果在代码中这么做:

package idroidgame.ActivityTest;



import android.app.Activity;

import android.os.Bundle;



import android.widget.TextView;

public class ActivityTest extends Activity {

	TextView tv=null;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        tv=(TextView)this.findViewById(R.id.tvHelloWorld); String str="<a href='http://www.xnadevelop.com'></a>"; tv.setText(str);

    }

    

}

你会发现在手机中运行<a href='http://www.xnadevelop.com'></a> 会原模原样输出。

需要在程序中显示超链接可以这么做。

1.修改TextView的标记增加android:autoLink=”all”

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView

	android:id="@+id/tvHelloWorld" 

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="@string/hello"

 android:autoLink="all"

    />

</LinearLayout>

不需要写a标记,应用程序会根据http://判断是否将该文本设置为超链接

tv=(TextView)this.findViewById(R.id.tvHelloWorld);

 String str="http://www.xnadevelop.com";

tv.setText(str);

2.不需要在布局文件中修改TextView,在程序中实现:

package idroidgame.ActivityTest;



import android.app.Activity;

import android.os.Bundle;



import android.text.util.Linkify;

import android.widget.TextView;

public class ActivityTest extends Activity {

	TextView tv=null;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        tv=(TextView)this.findViewById(R.id.tvHelloWorld);

        tv.setAutoLinkMask(Linkify.ALL);         String str="http://www.xnadevelop.com";
tv.setMovementMethod(LinkMovementMethod.getInstance()); tv.setText(str); } }
Linkify有很多选项 image ,对应在布局文件中

的选项image

3.格式化输出文本

package idroidgame.ActivityTest;



import android.app.Activity;

import android.os.Bundle;



import android.widget.TextView;

import android.text.Html;

public class ActivityTest extends Activity {

	TextView tv=null;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        tv=(TextView)this.findViewById(R.id.tvHelloWorld);



 String str="<a href='http://www.xnadevelop.com'>xnadevelop.com</a>"; tv.setMovementMethod(LinkMovementMethod.getInstance()); tv.setText(Html.fromHtml(str));

    }

    

}

这时候与前两个方法不同,要输出超链接不许指定A标记。

4.用Spannable

package idroidgame.ActivityTest;



import android.app.Activity;

import android.os.Bundle;



import android.widget.TextView;

import android.text.SpannableString;

import android.text.Spanned;

import android.text.style.URLSpan;

public class ActivityTest extends Activity {

	TextView tv=null;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        tv=(TextView)this.findViewById(R.id.tvHelloWorld);



        

        SpannableString ss=new SpannableString("http://xnadevelop.com"); ss.setSpan(new URLSpan("http://www.xnadevelop.com"),0, 21,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        tv.setMovementMethod(LinkMovementMethod.getInstance());

        tv.setText(ss);

    }

    

}
该方法只能显示超链接样式,不能点。

 

注意:2,3,4方法必须加入tv.setMovementMethod(LinkMovementMethod.getInstance());

你可能感兴趣的:(textview)