SpannableString实现富文本

最近需要实现类似微博的转发@某人,,点击某人调转个人中心

public class MainActivity extends AppCompatActivity {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.text);
        SpannableString spannableString = new SpannableString("哈哈@//嘿嘿吼吼@//嘿咻嘻嘻啦啦啦摸摸");
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.BLUE);
                ds.setUnderlineText(true);
            }
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "111111", LENGTH_LONG).show();
            }
        };
        ClickableSpan clickableSpan2 = new ClickableSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.BLUE);
                ds.setUnderlineText(true);
            }
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "2222222", LENGTH_LONG).show();
            }
        };
        spannableString.setSpan(clickableSpan, 5, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(clickableSpan2, 10,14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(spannableString);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

简单用法,如上先mark,后续完善更多用法

你可能感兴趣的:(SpannableString实现富文本)