MainActivity.java

package com.example.android_editext_1face;import java.lang.reflect.Field;import java.util.Random;import android.os.Bundle;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.text.Spannable;import android.text.SpannableString;import android.text.style.ImageSpan;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {    private EditText edittext;    private Button button;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        edittext = (EditText) this.findViewById(R.id.edittext);        button = (Button) this.findViewById(R.id.button);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                int randomId = 1 + new Random().nextInt(1);                try {                    Toast.makeText(MainActivity.this, "Hello", 0);                    Field f = R.drawable.class.getDeclaredField("face"                            + randomId);                    int resourceId = Integer.parseInt(f.get(null).toString());                    // 在android中要想显示图片信息,必须使得Bitmap的对象来装载                    Bitmap bitmap = BitmapFactory.decodeResource(                            getResources(), resourceId);                    // ImageSpan                    ImageSpan p_w_picpathspan = new ImageSpan(MainActivity.this,                            bitmap);                    SpannableString spannablestring = new SpannableString(                            "face");                    spannablestring.setSpan(p_w_picpathspan, 0, 4,                            Spannable.SPAN_EXCLUSIVE_INCLUSIVE);                    // 添加内容到edittext中                    edittext.append(spannablestring);                } catch (Exception e) {                    // TODO: handle exception                }            }        });    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }}

activity_main.xml

      

android_常用UI控件_02_EditText_01添加图片到edittext中_第1张图片

更多相关文章

  1. Android 图片点击 全屏缩放预览
  2. Android富文本图文混排、粗体、斜体、图片、下划线、删除线等
  3. android调用系统(相机)的图片,并且返回
  4. Android之十三图片2D翻转
  5. Android 编程下背景图片适配工具类
  6. android 照相以及关于自动生成的图片分辨率和面积太小问题
  7. 【OOM】Android加载大图片OOM异常解决

随机推荐

  1. Android实现局域网组播
  2. Common Intents
  3. 接口 登录、注册、信息
  4. android自定义一圆角ImageView
  5. 调用摄像头拍照并显示
  6. android 读中文文本文件
  7. Android抽屉(SlidingDrawer)的实现
  8. Android中的数据存储方式
  9. Android中获取全局Context
  10. 单条-多条 折线图