164814330  

範例下載

 

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 imageSpan = new ImageSpan(MainActivity.this,
                            bitmap);
                    SpannableString spannableString = new SpannableString("f00");
                    spannableString.setSpan(imageSpan, 03,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    // 添加文字
                    ev.append(spannableString);
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

 

 

本文出自 “天马行空” 博客,请务必保留此出处http://shaotao.blog.51cto.com/7606729/1332498

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()