package com.example.urlspantextviewclick;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(TextView)findViewById(R.id.txt);
CharSequence str=getText(R.string.hello_world);
SpannableString spannableString1 = new SpannableString(str);
spannableString1.setSpan(new ClickableSpan(){
@Override
public void onClick(View widget) {
Toast.makeText(getApplicationContext(), "who hit me", Toast.LENGTH_SHORT).show();
}
}, str.length()-3, str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString1.setSpan(new ForegroundColorSpan(Color.RED),str.length()-3, str.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
t1.setText(spannableString1);
t1.setMovementMethod(LinkMovementMethod.getInstance());
//Html.fromHtml(source)
//spannableString1.
/**to see http://www.cnblogs.com/mxgsa/archive/2012/11/15/2760256.html
* Android TextView 支持的HTML标签
•<a href="...">
•<b>
•<big>
•<blockquote>
•<br>
•<cite>
•<dfn>
•<div align="...">
•<em>
•<font size="..." color="..." face="...">
•<h1>
•<h2>
•<h3>
•<h4>
•<h5>
•<h6>
•<i>
•<img src="...">
•<p>
•<small>
•<strike>
•<strong>
•<sub>
•<sup>
•<tt>
•<u>
*/
}
@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;
}
}