TextView中的可单击Word? Android的

时间:2012-03-14 01:19:11

标签: android textview

有没有办法在TextView中突出显示单词,这样您就可以单击它并启动其他活动?我想制作一种字典App和定义中的一些Word不是自我解释的,所以我想当我突出它们并且用户可以点击它们时,另一个活动应该从另一个定义开始。

感谢。

2 个答案:

答案 0 :(得分:0)

给你一个演示:

public class TextViewDemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            TextView txtInfo = new TextView(this);
            SpannableString ss = new SpannableString("helloworldandroid:.");
            ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss.setSpan(new URLSpan("tel:4155551212"), 2, 5,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 5, 7,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss.setSpan(new StrikethroughSpan(), 7, 10,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss.setSpan(new UnderlineSpan(), 10, 16,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ss.setSpan(new ForegroundColorSpan(Color.GREEN), 10, 15,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
            ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
            ss.setSpan(span, 18, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            txtInfo.setText(ss);
            txtInfo.setMovementMethod(LinkMovementMethod.getInstance());
            setContentView(txtInfo);
    }

}

答案 1 :(得分:0)

试试这个..

    public class TextViewDemo extends Activity { 
        /** Called when the activity is first created. */ 
        @Override 
        public void onCreate(Bundle savedInstanceState) { 
                super.onCreate(savedInstanceState); 
                setContentView(R.layout.main);
         TextView textdata = (TextView) findViewById(R.id.textdata);
          // To Display the Text as Link
            textdata .setText(Html
                    .fromHtml("<a href='Write Ur Text Here'>Write Ur Text Here</a>"));
            textdata .setMovementMethod(LinkMovementMethod.getInstance());

            // Performing action on TouchListener of ForgotPassword
            textdata .setOnTouchListener(new View.OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    Intent Forgotpwdintent = new Intent();
                    Forgotpwdintent.setClass(TextViewDemo .this,
                            TargetActivity.class);
                    startActivity(Forgotpwdintent);
                    return true;

                }
            });
}
}