为什么不在文字中显示两张图片?

时间:2013-04-05 13:10:51

标签: java android eclipse text image

当我在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);

                      }  
                  }

0 个答案:

没有答案