TextView的其他使用

来源:互联网 发布:软件许可管理 编辑:程序博客网 时间:2024/06/04 19:22

Textview的使用

类似是微博的点击#内容后#的一些方法和技巧

public class TextViewMainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.textviewmainactivity);        init();    }    private void init() {        TextView textViewResource = (TextView) findViewById(R.id.text_html_resource);        textViewResource.setText(                Html.fromHtml(getResources().getString(R.string.link_text_manual)));        textViewResource.setMovementMethod(LinkMovementMethod.getInstance());//        TextView textViewHtml = (TextView) findViewById(R.id.text_html_program);        textViewHtml.setText(                Html.fromHtml(                        "<b>text_html_program: Constructed from HTML programmatically.</b>"                                + "  Text with a <a href=\"http://www.google.com\">link</a> "                                + "created in the Java source code using HTML."));        textViewHtml.setMovementMethod(LinkMovementMethod.getInstance());//        SpannableString ss = new SpannableString(                "text_spannable: Manually created spans. Click here to dial the phone.");        ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 39,                Spanned.SPAN_INCLUSIVE_INCLUSIVE);        ss.setSpan(new URLSpan("tel:4155551212"), 40 + 6, 40 + 10,                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//        TextView textViewSpan = (TextView) findViewById(R.id.text_spannable);        String string = "#hello#what is yout ##name ,my name is #Yello#";        SpannableString spStr = new SpannableString(string);        Pattern pattern = Pattern.compile("#[^#]+#");        Matcher matcher = pattern.matcher(string);        boolean result = matcher.find();        if (result) {            do {                MyClickSpan clickSpan = new MyClickSpan(this,matcher.group()); //设置超链接                spStr.setSpan(clickSpan, matcher.start(), matcher.end(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);            }while (matcher.find());            textViewSpan.append(spStr);            textViewSpan.setMovementMethod(LinkMovementMethod.getInstance());        } else {            textViewSpan.setText("匹配失败");        }    }    private class MyClickSpan extends ClickableSpan {        private Context context;        private String string;        public MyClickSpan(Context context,String string) {            super();            this.context = context;            this.string = string;        }        @Override        public void updateDrawState(TextPaint ds) {            super.updateDrawState(ds);            ds.setColor(ds.linkColor);            ds.setUnderlineText(false);        }        @Override        public void onClick(View widget) {//            Intent intent = new Intent(context, MainActivity.class);//            startActivity(intent);            Toast.makeText(context,string,Toast.LENGTH_LONG).show();        }    }}

布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    >    <TextView        style="@style/LinkText"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/intro" />    <!-- text_auto_linkify automatically linkifies things like URLs and phone numbers. -->    <TextView        android:id="@+id/text_auto_linkify"        style="@style/LinkText"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:autoLink="all"        android:text="@string/link_text_auto" />    <!--           text_html_resource uses a string resource containing explicit anchor tags (<a>)           to specify links.    -->    <TextView        android:id="@+id/text_html_resource"        style="@style/LinkText"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <!-- text_html_program builds the text in the Java code using HTML. -->    <TextView        android:id="@+id/text_html_program"        style="@style/LinkText"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <!-- text_spannable builds the text in the Java code without using HTML. -->    <TextView        android:id="@+id/text_spannable"        style="@style/LinkText"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>

下面的是字符values string

<resources>    <string name="app_name">test1</string>    <string name="hello_world">Hello world!</string>    <string name="action_settings">Settings</string>     <string name="intro">This sample illustrates how links can be added to a TextView.    \nThis can be done either automatically by setting the <i>autoLink</i> property    or explicitly.</string>    <string name="link_text_auto"><b>text_auto_linkify: Various kinds      of data that will be auto-linked.</b>      In this text are some things that are actionable.  For instance,      you can click on http://www.google.com and it will launch the      web browser.  You can click on google.com too.  If you      click on (415) 555-1212 it should dial the phone.  Or just write      foobar@example.com for an e-mail link.  If you have a URI like      http://www.example.com/lala/foobar@example.com you should get the      full link not the e-mail address.  Or you can put a location      like 1600 Amphitheatre Parkway, Mountain View, CA 94043.  To summarize:      https://www.google.com, or 650-253-0000, somebody@example.com,      or 9606 North MoPac Expressway, Suite 400, Austin, TX 78759.</string>    <string name="link_text_manual"><![CDATA[<b>text_html_resource:      Explicit links using &lt;a&gt; markup.</b>      This has markup for a <a href="http://www.google.com">link</a> specified      via an &lt;a&gt; tag.  Use a \"tel:\" URL      to <a href="tel:4155551212">dial a phone number</a>.]]></string></resources>

总感觉贴代码的感觉好麻烦……
主要是实现textview点击后可能出现的效果
效果图
这里写图片描述

0 0