public class Tv2Activity extends Activity implements OnClickListener {
private EditText et;
private Button bt;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) this.findViewById(R.id.et);
bt = (Button) this.findViewById(R.id.bt);
bt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// 通过反射得到资源id;
Integer inte = new Random().nextInt(7) + 1;
try {
Field field = R.drawable.class.getDeclaredField("widget0" + inte);
int resourId = Integer.parseInt(field.get(null).toString());
// 得到图片资源
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourId);
// 用图片去替换字就要用到ImageSpan
ImageSpan imageSpan = new ImageSpan(this, bitmap);
SpannableString ss = new SpannableString("tupian");
//这里的0-6就是tupian这个字符长度。用图片去替换这个字符。
ss.setSpan(imageSpan, 0, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
et.append(ss);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private EditText et;
private Button bt;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) this.findViewById(R.id.et);
bt = (Button) this.findViewById(R.id.bt);
bt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// 通过反射得到资源id;
Integer inte = new Random().nextInt(7) + 1;
try {
Field field = R.drawable.class.getDeclaredField("widget0" + inte);
int resourId = Integer.parseInt(field.get(null).toString());
// 得到图片资源
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourId);
// 用图片去替换字就要用到ImageSpan
ImageSpan imageSpan = new ImageSpan(this, bitmap);
SpannableString ss = new SpannableString("tupian");
//这里的0-6就是tupian这个字符长度。用图片去替换这个字符。
ss.setSpan(imageSpan, 0, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
et.append(ss);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}