使用Spans替换多个实例

时间:2013-07-01 00:01:33

标签: android textview

我使用下面的代码更改[text]带图像的实例。到目前为止,我所拥有的代码根据需要替换了第一个实例,但忽略了每个其他实例。我想从字符串的内容主体更改[text]的每个实例,例如:

Hello this is [text] a test [test] sample of input [text] string.括号内的文字每次都可能不同。

代码:

public SpannableStringBuilder smileyConvert(String msgBody) {
    SpannableStringBuilder ssb = new SpannableStringBuilder(msgBody);


    if( msgBody.contains("[") && msgBody.contains("]") ){
        setStartEnd(msgBody);

        AssetManager am = ctx.getAssets();
        InputStream imgStream = null;
        try{
            imgStream = am.open("emotes/" + ico + ".gif");
        }catch(IOException e){
            e.printStackTrace();
        }
        Bitmap smiley = BitmapFactory.decodeStream(imgStream);
        smiley = sizeBitmap(smiley);
        ssb.setSpan(new ImageSpan(ctx, smiley), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return ssb;
    }else
        return ssb;
}


 public void setStartEnd(String msgBody) {

    start = msgBody.indexOf("[");
    end = (msgBody.indexOf("]") + 1);


    return;
}

任何建议提示都将受到赞赏。

0 个答案:

没有答案
相关问题