在EditText中输入用户名时标记用户

时间:2017-12-27 17:54:18

标签: android tags

我希望在' @'之后复制标记用户的功能,同时在编辑文本中输入他的名字。已输入

请参阅附图,我想要做什么

enter image description here

在这里,sally jones在编辑文本中输入他的名字时标记了chris remkes。

任何建议如何执行此操作,以便在单击该名称时,我可以显示用户详细信息。

1 个答案:

答案 0 :(得分:0)

您可以使用ClickableSpan:

SpannableString textString = new SpannableString("Great video @ChrisRemkes I'm making a similar one but I think it could do with some editing like your one!!!");
ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View textView) {
        Toast.makeText(getApplicationContext(), "The text '@ChrisRemkes' has been clicked. Do something.",
                       Toast.LENGTH_SHORT).show();
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        // NOTE: here's where you can set the styling give UI feedback that this is text that can be clicked on. 
        ds.setColor(getResources().getColor(android.R.color.holo_blue));
    }
};
// NOTE: you will need to setup getStartPositionOfTag(textString) and getEndPositionOfTag(textString) to pattern match on the textString to find the start position and end position of @ChrisRemkes
textString.setSpan(clickableSpan, getStartPositionOfTag(textString), getEndPositionOfTag(textString), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(textString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
相关问题