public class MainActivity extends Activity {
private EditText ev;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ev = (EditText) findViewById(R.id.ev);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int randomId = new Random().nextInt(10);// 产生0--9的随机数
try {
Field field = R.drawable.class.getDeclaredField("f00"
+ randomId);
int resourid = Integer.parseInt(field.get(null).toString());
Bitmap bitmap = BitmapFactory.decodeResource(
getResources(), resourid);
ImageSpan p_w_picpathSpan = new ImageSpan(MainActivity.this,
bitmap);
SpannableString spannableString = new SpannableString("f00");
spannableString.setSpan(p_w_picpathSpan, 0, 3,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// 添加文字
ev.append(spannableString);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
转载于:https://blog.51cto.com/shaotao/1332498