android editText 插入表情,或者是图片

分类: android 112人阅读 评论(1) 收藏 举报

在android 的editText 中插入表情,其实就是将一个自己定义好的特殊的标示使用图片进行替换:

首先你要自己定义一个类继承EditText 这个类

view plain copy to clipboard print ?
  1. packagecom.work.down;
  2. importandroid.content.Context;
  3. importandroid.graphics.drawable.Drawable;
  4. importandroid.text.Spannable;
  5. importandroid.text.SpannableStringBuilder;
  6. importandroid.text.style.ImageSpan;
  7. importandroid.util.AttributeSet;
  8. importandroid.util.Log;
  9. importandroid.widget.EditText;
  10. publicclassImageViewsextendsEditText{
  11. publicImageViews(Contextcontext){
  12. super(context);
  13. }
  14. publicImageViews(Contextcontext,AttributeSetattrs,intdefStyle){
  15. super(context,attrs,defStyle);
  16. }
  17. publicImageViews(Contextcontext,AttributeSetattrs){
  18. super(context,attrs);
  19. }
  20. publicvoidinsertIntoEdit(intid){
  21. Log.d("wljie","===");
  22. SpannableStringBuilderbuilder=newSpannableStringBuilder(getText().toString());
  23. Drawabledrawable=getResources().getDrawable(id);
  24. drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
  25. ImageSpanimageSpan=newImageSpan(drawable,ImageSpan.ALIGN_BASELINE);
  26. builder.setSpan(imageSpan,getText().length(),getText().length()+"[simle]".length(),Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
  27. setText(builder);
  28. }
  29. }
package com.work.down;import android.content.Context;import android.graphics.drawable.Drawable;import android.text.Spannable;import android.text.SpannableStringBuilder;import android.text.style.ImageSpan;import android.util.AttributeSet;import android.util.Log;import android.widget.EditText;public class ImageViews extends EditText {public ImageViews(Context context) {super(context);}public ImageViews(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public ImageViews(Context context, AttributeSet attrs) {super(context, attrs);}public void insertIntoEdit(int id){Log.d("wljie","===");SpannableStringBuilder builder = new SpannableStringBuilder(getText().toString());Drawable drawable = getResources().getDrawable(id);drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);builder.setSpan(imageSpan, getText().length(),getText().length()+"[simle]".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);setText(builder);}}

然后在mail.xml中写入

view plain copy to clipboard print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <Button
  7. android:id="@+id/button"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="click"
  11. />
  12. <com.energysource.szj.embeded.AdView
  13. android:id="@+id/adview_ids"
  14. transparent="true"
  15. debug="true"
  16. android:layout_width="240px"
  17. android:layout_height="38px"
  18. android:layout_marginLeft="0px"
  19. android:layout_marginTop="0px"
  20. />
  21. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="click" /> <com.energysource.szj.embeded.AdView android:id="@+id/adview_ids"transparent="true"debug="true"android:layout_width="240px"android:layout_height="38px"android:layout_marginLeft="0px"android:layout_marginTop="0px"/></LinearLayout>

然后才MainActivity.java中

view plain copy to clipboard print ?
  1. ImageViewsimageViews=(ImageViews)findViewById(R.id.image_view);
  2. Log.e("wljie",R.drawable.simle+"++++^_^");
  3. imageViews.insertIntoEdit(R.drawable.simle);

更多相关文章

  1. Android应用程序添加自定义的property属性
  2. Android中的?attr/和?android:attr/
  3. Android画图学习总结(四)——Animation(下)
  4. Android(安卓)TextView、EditText显示表情
  5. Android踩坑日记:自定义水平和圆形ProgressBar样式
  6. Android自定义标题栏
  7. ANDROID Porting系列九、Lights
  8. Android中自定义SeekBar来控制音量,并与系统音量键的操作保持同步
  9. Android(安卓)ListView元素间隙线自定义渐变效果

随机推荐

  1. Android(安卓)resource linking failed e
  2. Android实现局部图片滑动指引效果
  3. android 获取服务器文件流 hander
  4. Android底部导航栏
  5. Android(安卓)Activity生命週期简介
  6. Android学习博客和文章存档
  7. Android:启动页有短暂白屏的解决方法
  8. 管道的私用,删除android的流氓
  9. android 修改videoview的宽度和高度
  10. Consider adding android:paddingStart=