Is there any example about Spanned and Spannable text

前端 未结 2 544

I\'m struggling with using EditText and Spannable text object, These days, I\'ve read API documents around ten times, even I\'m not certain that I understand correctly. So I

2条回答
  •  粉色の甜心
    2020-11-28 08:50

    Since you don't specify what you can't grasp from the API it's hard to answer your questions (short answer: rewrite your question to a specific questions rather than a general one).

    A typical Spannable-example is something like this to turn selected text in an EditText into Italic:

    Spannable str = mBodyText.getText(); 
    if(mBodyText.getSelectionEnd() > mBodyText.getSelectionStart()) 
      str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC),  
                          mBodyText.getSelectionStart(), mBodyText.getSelectionEnd(),  
                          Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    else
      str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC),
                  mBodyText.getSelectionEnd(),
                  mBodyText.getSelectionStart(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    

    This is cut and pasted from something else, so your direct-pastability might have suffered, but it at least shows a working example of a Spannable (in this case a StyleSpan). In the API you can find the other types of Spans (notably ImageSpan, which is a common questions among newly converted droiders).

提交回复
热议问题