android editText 插入表情,或者是图片
分类: android
2011-06-07 10:42
112人阅读
收藏
举报
在android 的editText 中插入表情,其实就是将一个自己定义好的特殊的标示使用图片进行替换:
首先你要自己定义一个类继承EditText 这个类
view plain copy to clipboard print ?
- 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);
- }
-
-
-
- }
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 ?
- <?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>
<?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 ?
- ImageViews imageViews = (ImageViews) findViewById(R.id.image_view);
- Log.e("wljie", R.drawable.simle+"++++^_^");
- imageViews.insertIntoEdit(R.drawable.simle);