android_常用UI控件_02_EditText_01添加图片到edittext中
MainActivity.java
创新互联长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为盘龙企业提供专业的网站制作、成都网站设计,盘龙网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。
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中
标题路径:http://scyanting.com/article/gihopj.html