如何为字符串中的特定单词指定下划线和颜色,并在该单词上给出单击事件?

时间:2014-01-02 13:19:22

标签: java android

字符串是

Or Login to Place your Order

我想要Login不同颜色的单词加下划线。还需要click event

上的Login

我想用单TextView来做。我可以使用单TextView吗?

2 个答案:

答案 0 :(得分:1)

强调特定字词......

TextView t = (TextView) findViewById(R.id.textview);
t.setPaintFlags(t.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
t.setText("Underline Text");

以下是您要求的example

答案 1 :(得分:0)

我得到了答案

String orLoginString = "Or Login to Place your Order";

SpannableString redSpannable= new SpannableString(orLoginString);

ClickableSpan clickableSpan = new ClickableSpan() 
{

       @Override
       public void onClick(View textView) {

              //Your code onclick of underline string

        }

        @Override
        public void updateDrawState(TextPaint ds) {
             // TODO Auto-generated method stub
             super.updateDrawState(ds);
             //You change the color of underline string. 
             //You can make underline visible or invisible
        }
};  


redSpannable.setSpan(clickableSpan, 3, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

yourtextview.setText(redSpannable);

loginSelectionFirstTimeLaunching.setMovementMethod(LinkMovementMethod.getInstance());