我在互联网上到处搜索,但是没有找到任何文档来将样式化的文本从我的应用程序发送到Whatsapp。
这是我用来通过意图将文本从应用程序发送到Whatsapp的代码:
String s = list.get(index);
SpannableStringBuilder SS = new SpannableStringBuilder(s);
int length = s.length();
SS.setSpan (new CustomTypefaceSpan("", custom_font), 0, length-1,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, SS);
try {
ctx.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ctx, "Whatsapp is not installed!", Toast.LENGTH_SHORT).show();
}
这是CustomTypefaceSpan类:
public class CustomTypefaceSpan extends TypefaceSpan {
private final Typeface newType;
public CustomTypefaceSpan(String family, Typeface type) {
super(family);
newType = type;
}
@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, newType);
}
@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, newType);
}
private static void applyCustomTypeFace(Paint paint, Typeface tf) {
int oldStyle;
Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
int fake = oldStyle & ~tf.getStyle();
if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fake & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
paint.setTypeface(tf);
}
}
它仅发送纯文本,而未应用样式。 预先感谢。