当我在 text1 中输入 S 时,相应的图片会出现在 text2 中,但是当我在 text1 中输入 G 时,相应的图片会显示在 text2 中,但是 S 的前一张图片显示为字母而不是图片。这是为什么?为什么不能显示两张图片?怎么了?
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText te1 = (EditText)findViewById(R.id.t1);
final EditText te2 = (EditText)findViewById(R.id.t2);
final Button v = (Button)findViewById(R.id.b1);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//imva.setImageResource(R.id.b1);
te2.setText(" ");
String t= te1.getText().toString();
char [] aa = t.toString().toCharArray();
for (int i = 0 ; i < aa.length ; i++)
{
if (aa[i] == 's')
{
SpannableStringBuilder builder = new SpannableStringBuilder(te1.getText());
do {
ImageSpan imageSpan = new ImageSpan(getBaseContext(),R.drawable.a1);
int pos = builder.toString().indexOf("s");
builder.replace(pos, pos + 1, "$");
builder.setSpan(imageSpan, pos, pos + 1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} while (builder.toString().indexOf("s") > -1);
te2.setText(builder);
}
if (aa[i] == 'g')
{
SpannableStringBuilder builder = new SpannableStringBuilder(te1.getText());
do {
ImageSpan imageSpan = new ImageSpan(getBaseContext(),R.drawable.a2);
int pos = builder.toString().indexOf("g");
builder.replace(pos, pos+ 1, "$");
builder.setSpan(imageSpan, pos, pos + 1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} while (builder.toString().indexOf("g") > -1);
te2.setText(builder);
}
}