2017-12-27 650 views
1

我想複製標記用戶,而在之後編輯文本輸入他的名字命名的功能,輸入用戶名,當用戶「@」進入標籤中的EditText

請參閱所附的圖像正是我想要什麼做

enter image description here

下面部的Sally Jones標記,而從編輯的文本輸入他的名字克里斯remkes。

任何建議如何做到這一點,所以當這個名字被點擊時,我可以顯示用戶的詳細信息。

回答

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