android editText 插入表情,或者是图片
16lz
2021-12-04
android editText 插入表情,或者是图片
分类: android 2011-06-07 10:42 112人阅读 评论(1) 收藏 举报在android 的editText 中插入表情,其实就是将一个自己定义好的特殊的标示使用图片进行替换:
首先你要自己定义一个类继承EditText 这个类
view plain copy to clipboard print ?- packagecom.work.down;
- importandroid.content.Context;
- importandroid.graphics.drawable.Drawable;
- importandroid.text.Spannable;
- importandroid.text.SpannableStringBuilder;
- importandroid.text.style.ImageSpan;
- importandroid.util.AttributeSet;
- importandroid.util.Log;
- importandroid.widget.EditText;
- publicclassImageViewsextendsEditText{
- publicImageViews(Contextcontext){
- super(context);
- }
- publicImageViews(Contextcontext,AttributeSetattrs,intdefStyle){
- super(context,attrs,defStyle);
- }
- publicImageViews(Contextcontext,AttributeSetattrs){
- super(context,attrs);
- }
- publicvoidinsertIntoEdit(intid){
- Log.d("wljie","===");
- SpannableStringBuilderbuilder=newSpannableStringBuilder(getText().toString());
- Drawabledrawable=getResources().getDrawable(id);
- drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
- ImageSpanimageSpan=newImageSpan(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 ?- <?xmlversion="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 ?- ImageViewsimageViews=(ImageViews)findViewById(R.id.image_view);
- Log.e("wljie",R.drawable.simle+"++++^_^");
- imageViews.insertIntoEdit(R.drawable.simle);
更多相关文章
- Android应用程序添加自定义的property属性
- Android中的?attr/和?android:attr/
- Android画图学习总结(四)——Animation(下)
- Android(安卓)TextView、EditText显示表情
- Android踩坑日记:自定义水平和圆形ProgressBar样式
- Android自定义标题栏
- ANDROID Porting系列九、Lights
- Android中自定义SeekBar来控制音量,并与系统音量键的操作保持同步
- Android(安卓)ListView元素间隙线自定义渐变效果