Why can't I use the same Span to install twice twice?
SpannableString ss = new SpannableString ("aaaaa [1] bbbb [1] cccc [1]");
I need to replace all [1] with the image. If I use the following code, only the latter is replaced by an image:
etShow = (EditText) findViewById(R.id.show); SpannableString ss = new SpannableString("aaaaa[1]bbbb[1]cccc[1]"); int[] starts = new int[3]; int[] ends = new int[3]; int h = 0; int k = 0; for (int i = 0; i < ss.length(); i++) { if (ss.charAt(i) == '[') { starts[h] = i; h++; } else if (ss.charAt(i) == ']') { ends[k] = i; k++; } } Drawable d = getResources().getDrawable(R.drawable.ic_launcher); d.setBounds(0, 0, 50, 50); ImageSpan im = new ImageSpan(d); for(int i=0;i<3;i++){ ss.setSpan(im, starts[i], ends[i]+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); } etShow.getText().insert(0, ss);
If you change the following code, all [1] are replaced by the image.
Drawable d = getResources().getDrawable(R.drawable.ic_launcher); d.setBounds(0, 0, 50, 50); ImageSpan im = new ImageSpan(d); ImageSpan im1 = new ImageSpan(d); ImageSpan im2 = new ImageSpan(d); //for(int i=0;i<3;i++){ // ss.setSpan(im, starts[i], ends[i]+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); ss.setSpan(im, starts[0], ends[0]+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); ss.setSpan(im1, starts[1], ends[1]+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); ss.setSpan(im2, starts[2], ends[2]+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); // }
How to explain this?
source share