2012年6月17日 星期日

Spannable 文章的每一個字顯示其義


希望按下一段英文文章的每一個字都能顯示其意思。

利用 Spannable 和 ClickableSpan




public class Cq_Spannable01Activity extends Activity {
    /** Called when the activity is first created. */

private TextView tv;
//private ListView lv;
private static Context ctx;

private String str = "I love you, but she doesn't love him.";
private String [] strings;

private String result = ""; //每個元素 加上 href 之後

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
   /** Test the displaying of the components in a string **/
       // lv = (ListView) findViewById(R.id.listView1);
        strings = TextUtils.split(str, " ");
        //clear the empty space
        for (int i = 0; i< strings.length; i++) {
        String temp = "";
        String temp02 = "";
        temp = strings[i].trim();
        if (temp.endsWith(".") || temp.endsWith(",")){
        temp02 = temp.substring(0, temp.length()-1);
        temp = temp02;
       
        }
        temp = "<a style='color:yellow' href='" + temp + "'> " + temp + "</a>";
       
        strings[i] = temp;
        }
     
//        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);
//        lv.setAdapter(adapter);
        /** Test the displaying of the components in a string **/
     
     
        for (int i = 0; i<strings.length; i++) {
        result += strings[i];
        }
     
     
     
     
        tv = (TextView) findViewById(R.id.textview);
        tv.setTextSize(25);
        ctx = this;
     
        String htmlLinkText = "";
//        htmlLinkText = "aaaaaaaaaa"
//         + "<a style='color:red' href='我是超連結...'>請按超連結</a>"
//         + "aaaaaaaaaaaaaaaa";
     
     
     
        htmlLinkText = "aaaaaaaaaaa"
        + "<a style='color:yellow' href='我'> I</a>"
        + "<a style='color:red' href='愛'> love</a>"
        + "<a style='color:green' href='妳'> you. </a>"
        + "aaaaaaaaaaaaaaaa";
     
     
     
     
        //tv.setText(Html.fromHtml(htmlLinkText));
        tv.setText(Html.fromHtml(result));
        tv.setMovementMethod(LinkMovementMethod.getInstance());
     
        CharSequence text = tv.getText();
        if (text instanceof Spannable) {
        int end = text.length();
        Spannable sp = (Spannable) tv.getText();
        URLSpan[] urlspan = sp.getSpans(0, end, URLSpan.class);
       
        SpannableStringBuilder style = new SpannableStringBuilder(text);
        style.clearSpans();
        for (URLSpan url : urlspan) {
        MyURLSpan myURLSpan = new MyURLSpan(url.getURL());
        style.setSpan(myURLSpan, sp.getSpanStart(url), sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
       
        tv.setText(style);
        }
     
    }
 
 
    private static class MyURLSpan extends ClickableSpan {
    private String mUrl;
    private String temp;
   
    MyURLSpan(String url) {
    mUrl = url;
   
    }

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

//widget.setBackgroundColor(Color.parseColor("#00000000"));

if (mUrl.equalsIgnoreCase("I")) {
mUrl = "我";
}else if (mUrl.equalsIgnoreCase("love")) {
mUrl = "愛";

}else if (mUrl.equalsIgnoreCase("you")) {
mUrl = "妳";
}else if (mUrl.equalsIgnoreCase("but")) {
mUrl = "但是";
}else if (mUrl.equalsIgnoreCase("she")) {
mUrl = "她";
}else if (mUrl.equalsIgnoreCase("doesn't")) {
mUrl = "不";
}else if (mUrl.equalsIgnoreCase("him")) {
mUrl = "他";
}

Toast.makeText(ctx, mUrl, Toast.LENGTH_SHORT).show();
}
   
    } //end of class myURLSpan
}

沒有留言:

張貼留言