我希望能够将我的应用程序活动之一的格式化文本(即格式跨度的文本)发送到另一个.这些CharSequence实例深入Parcelable到我创建的某些类型中.
例如,当编组其中一个带有格式化CharSequences 的类型时,我使用TextUtils.writeToParcel如下:
public class HoldsCharSequence {
/* some formatted string that uses basic spans (e.g., ForegroundColorSpan) */
private CharSequence cs;
public void writeToParcel(Parcel out, int flags) {
TextUtils.writeToParcel(cs, out, 0);
}
/* ... rest of the code ... */
}
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何CharSequence从Parcel我的私有构造函数中检索.
以下就不能正常工作:
private HoldsCharSequence(Parcel in) {
cs = (CharSequence) in.readValue(CharSequence.class.getClassLoader());
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
android.os.BadParcelableException: ClassNotFoundException when unmarshalling
Run Code Online (Sandbox Code Playgroud)
还有两件事:1.我已经成功实现了自己的自定义Parcelable对象,CharSequence特别是问题.2.我知道TextUtils.writeToParcel在保存文本格式方面会做出最大努力,我可以忍受.
我通过我的Android手机单元将文本发送到蓝牙打印机.无论是打印机和我的设备通过蓝牙连接. 它工作正常,我在纸上得到了所需的文字.
我的问题是:
打印机采用默认font size的文本.我想更改要打印的文本的字体大小.
我怎么能实现这个?
这是我在bloutooth连接后打印文本的代码:
private void connect_print(BluetoothDevice bluetoothDevicess) {
// some code
printData();
// some code
}
Run Code Online (Sandbox Code Playgroud)
printData()方法
private void printData() {
// TODO Auto-generated method stub
String str = new String("This is the text sending to the printer");
String newline = "\n";
try {
out.write(str.getBytes(),0,str.getBytes().length);
Log.i("Log", "One line printed");
} catch (IOException e) {
Toast.makeText(BluetoothDemo.this, "catch 1", Toast.LENGTH_LONG).show();
e.printStackTrace();
Log.i("Log", "unable to write ");
flagCheck = false;
}
try {
out.write(newline.getBytes(),0,newline.getBytes().length);
} catch …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用此函数在Android中使用spannableString和正则表达式为部分字符串着色:
public static String StringReplace(String source) {
String find = "ABC";
SpannableString replace = new SpannableString(find);
replace.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
String output = source.replace(find, replace);
return output;
}
Run Code Online (Sandbox Code Playgroud)
因为函数replace()返回一个字符串,我无法获得彩色字符串.我的问题是:使用regexp为文本部分着色的最佳方法是什么?