0

我使用下面的代码来更改[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;
}

任何建议将不胜感激。

4

0 回答 0