要求:
1、editText放各种样式的文字,比如下划线、加粗等
解决方法是SpannableString类
2、editText放文字块,并且添加和删除都以块为单位,比如你添加“中国”,删除的时候也要一下删除这两个字
解决方法是:SpannableString类,将文字先转换为bitmap然后添加进SpannableString中
3、editText放图片,要求存数据的时候能存图片并图片在文本中的位置
解决方法是:SpannableString类,将图片命名,插入到editText中的位置,在获取它所有文字信息时根据名字就可获得图片所在的位置。
下面的例子中是查阅了网页很多资料,整理的,希望有用的朋友可以看看。
言归正传,实现步骤:
1、布局文件main.xml
android:layout_height="fill_parent"
android:background="#ff333333"
android:orientation="vertical" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:inputType="none"/>
android:layout_height="wrap_content"
>
布局文件的样式:
其中:中华人民共和国就是以块的形式插入的
2、activity类的实现
package com.liu.demo;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class SpannableStringActivity extends Activity implements
OnClickListener {
private EditText tv;
private Button underline_btn;
private Button strike_btn;
private Button style_btn;
private Button font_btn;
private Button color_btn1;
private Button color_btn2;
private Button url_btn;
private Button image_btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (EditText) this.findViewById(R.id.tv);
underline_btn = (Button) this.findViewById(R.id.underline_btn);
strike_btn = (Button) this.findViewById(R.id.strike_btn);
style_btn = (Button) this.findViewById(R.id.style_btn);
font_btn = (Button) this.findViewById(R.id.font_btn);
color_btn1 = (Button) this.findViewById(R.id.color_btn1);
color_btn2 = (Button) this.findViewById(R.id.color_btn2);
url_btn = (Button) this.findViewById(R.id.url_btn);
image_btn = (Button) this.findViewById(R.id.image_btn);
insertEditText("中国人民共和国");
underline_btn.setOnClickListener(this);
strike_btn.setOnClickListener(this);
style_btn.setOnClickListener(this);
font_btn.setOnClickListener(this);
color_btn1.setOnClickListener(this);
color_btn2.setOnClickListener(this);
url_btn.setOnClickListener(this);
image_btn.setOnClickListener(this);
}
//可通过Paint的measureText方法测量出文字所占的实际长度
//下面是生成图片的代码,最终生成的是椭圆形的图片,里面嵌套了文字
public static Bitmap createBitmap(Context context,String str){
int h = sp2px(context, 16);
Paint paint = new Paint();
paint.setTextSize(h);
int w = (int) paint.measureText(str);//计算文字实际占用的宽度
int height = 10+h;//将高度+10防止绘制椭圆时左右的文字超出椭圆范围
Bitmap bm = Bitmap.createBitmap(w+20, height, Config.ARGB_4444);
Canvas c = new Canvas(bm);
Paint p2 = new Paint();
RectF re = new RectF(5, 0, w+15, height);//矩形
float roundPx = 14;
p2.setAntiAlias(true);//设置Paint为无锯齿
c.drawARGB(0, 0, 0, 0);// 透明色
p2.setColor(context.getResources().getColor(R.color.bg_color));
c.drawRoundRect(re, roundPx, roundPx, p2);//绘制圆角矩形
Paint p1 = new Paint();
p1.setColor(Color.BLACK);
p1.setTextSize(sp2px(context, 16));
c.drawText(str, 10, 20, p1);
c.save(Canvas.ALL_SAVE_FLAG);
c.restore();
return bm;
}
/**
* 将sp值转换为px值,保证文字大小不变
*
* @param spValue
* @param fontScale
* (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int sp2px(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
private void insertEditText(String str) {
Bitmap bm = createBitmap(this, str);
if (bm != null) {
ImageSpan imageSpan = new ImageSpan(this, bm);
SpannableString spannableString = new SpannableString("[name]"
+ str + "[/name]");
spannableString.setSpan(imageSpan, 0,
("[name]" + str + "[/name]").length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
int index = tv.getSelectionStart();
Editable editable = tv.getEditableText();
if (index < 0 || index >= editable.length()) {
editable.append(spannableString);
} else {
editable.insert(editable.length(), spannableString);
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.underline_btn:
addUnderLineSpan();
break;
case R.id.strike_btn:
addStrikeSpan();
break;
case R.id.style_btn:
addStyleSpan();
break;
case R.id.font_btn:
addFontSpan();
break;
case R.id.color_btn1:
addForeColorSpan();
break;
case R.id.color_btn2:
addBackColorSpan();
break;
case R.id.url_btn:
addUrlSpan();
break;
case R.id.image_btn:
// addImageSpan();
setFace("我的图片",R.drawable.ic_launcher);
break;
}
}
//第一个参数 是图片要的名称,可以自己取 第二个参数就是 图片资源ID
private void setFace(String faceTitle, int faceImg) {
int start = tv.getSelectionStart();
Spannable ss = tv.getText().insert(start, "[" + faceTitle + "]");
Drawable d = getResources().getDrawable(faceImg);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, faceTitle + ".gif", ImageSpan.ALIGN_BASELINE);
ss.setSpan(span, start, start + ("[" + faceTitle + "]").length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
/**
* 图片
*/
private void addImageSpan() {
SpannableString spanString = new SpannableString(" ");
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
spanString.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(spanString);
System.out.println("onclick 图片:"+tv.getText().toString());
}
/**
* 超链接
*/
private void addUrlSpan() {
SpannableString spanString = new SpannableString("超链接");
URLSpan span = new URLSpan("tel:0123456789");
spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(spanString);
System.out.println("onclick 超链接:"+tv.getText().toString());
}
/**
* 文字背景颜色
*/
private void addBackColorSpan() {
SpannableString spanString = new SpannableString("颜色2");
BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);
spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(spanString);
System.out.println("onclick 文字背景颜色:"+tv.getText().toString());
}
/**
* 文字颜色
*/
private void addForeColorSpan() {
SpannableString spanString = new SpannableString("颜色1");
ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);
spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(spanString);
System.out.println("onclick 文字背景颜色:"+tv.getText().toString());
}
/**
* 字体大小
*/
private void addFontSpan() {
SpannableString spanString = new SpannableString("36号字体");
AbsoluteSizeSpan span = new AbsoluteSizeSpan(36);
spanString.setSpan(span, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(spanString);
System.out.println("onclick 字体大小:"+tv.getText().toString());
}
/**
* 粗体,斜体
*/
private void addStyleSpan() {
SpannableString spanString = new SpannableString("BIBI");
StyleSpan span = new StyleSpan(Typeface.BOLD_ITALIC);
spanString.setSpan(span, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(spanString);
System.out.println("onclick 粗体,斜体:"+tv.getText().toString());
}
/**
* 删除线
*/
private void addStrikeSpan() {
SpannableString spanString = new SpannableString("删除线");
StrikethroughSpan span = new StrikethroughSpan();
spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(spanString);
System.out.println("onclick 删除线:"+tv.getText().toString());
}
/**
* 下划线
*/
private void addUnderLineSpan() {
SpannableString spanString = new SpannableString("下划线");
UnderlineSpan span = new UnderlineSpan();
spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(spanString);
System.out.println("onclick 下划线:"+tv.getText().toString());
}
}
针对下面的插入信息及其获得的log,图片的名字叫:我的图片,没插入一次我都会打印一句:
log,根据"我的图片"就可以确定图片在文本中的位置: