简体   繁体   English

TextView中的可点击Word? 安卓系统

[英]Clickable Word in TextView? Android

Is there a way to highlight a word in a TextView, so you can click it and an other Activity starts? 有没有一种方法可以突出显示TextView中的单词,因此您可以单击它并启动另一个Activity? I want to make a kind of dictionary App and some Word in the definitions aren't self-explaining, so i thought when i highlight them and the user could click them, an other activity should start with an other definition. 我想制作一种词典App,并且定义中的某些Word不能自解释,因此我认为当我突出显示它们并且用户可以单击它们时,其他活动应从其他定义开始。

Thanks. 谢谢。

Give you a demo: 给你一个演示:

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);
    }

} }

Try With This.. 试试这个

    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;

                }
            });
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM