java保存可扩展字符串?
我正在创建一个notes应用程序,但我一直遇到一个关于可扩展字符串的问题
我试图实现的是,当我输入一个便笺并单击一个按钮时,我希望它在EditText
字段中添加一个大的粗体字符串,这是通过可扩展字符串实现的。我现在遇到的问题是,当我保存便笺并退出,然后返回便笺时,可跨越的字符串仍然存在,但不再是大的和粗体的
有没有办法保存可跨越的字符串?如果没有,有没有办法达到同样的效果
另请注意:我想知道是否有一种方法可以将可扩展字符串的文本设置为一个对象。所以当我擦除它时,它会一起擦除。不是每个字母都像普通文本
下面是我的NoteActivity中的按钮:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save_note:
save_note();
return true;
case 安卓.R.id.home:
save_note();
finish();
return true;
case R.id.add_intro:
SpannableString intro;
if (note_content.length() == 0) {
intro = new SpannableString("(Intro:)\n");
intro.setSpan(new StyleSpan(Typeface.BOLD), 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
intro.setSpan(new RelativeSizeSpan(1.25f), 0, 8, 0);
} else {
intro = new SpannableString("\n\n(Intro:)\n");
intro.setSpan(new StyleSpan(Typeface.BOLD), 0, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
intro.setSpan(new RelativeSizeSpan(1.25f), 0, 11, 0);
}
note_content.append(intro);
return true;
下面是如何保存和加载笔记 公用事业
public class Utilities {
public static final String FileExtention = ".bin";
public static boolean save_note(Context context, Note note) {
String fileName = String.valueOf(note.getDateTime()) + FileExtention;
FileOutputStream fos;
ObjectOutputStream oos;
try {
fos = context.openFileOutput(fileName, context.MODE_PRIVATE);
oos = new ObjectOutputStream(fos);
oos.writeObject(note);
oos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static ArrayList<Note> getAllSavedNotes(Context context) {
ArrayList<Note> notes = new ArrayList<>();
File filesDir = context.getFilesDir();
ArrayList<String> noteFiles = new ArrayList();
for(String file : filesDir.list()) {
if(file.endsWith(FileExtention)) {
noteFiles.add(file);
}
}
FileInputStream fis;
ObjectInputStream ois;
for(int i = 0; i < noteFiles.size(); i++) {
try{
fis = context.openFileInput(noteFiles.get(i));
ois = new ObjectInputStream(fis);
notes.add((Note)ois.readObject());
fis.close();
ois.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
return notes;
}
这是记事本
public class NoteAdapter extends ArrayAdapter<Note> {
public NoteAdapter(Context context, int resource, ArrayList<Note> notes) {
super(context, resource, notes);
}
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
//return super.getView(position, convertView, parent);
if(convertView == null) {
convertView = LayoutInflater.from(getContext())
.inflate(R.layout.item_note, null);
}
Note note = getItem(position);
if(note != null) {
TextView title = (TextView) convertView.findViewById(R.id.list_note_title);
TextView content = (TextView) convertView.findViewById(R.id.list_note_content);
TextView date = (TextView) convertView.findViewById(R.id.list_note_date);
Typeface music_font = Typeface.createFromAsset(getContext().getAssets(), "fonts/melodymakernotesonly.ttf");
Typeface scribble_card = Typeface.createFromAsset(getContext().getAssets(), "fonts/the unseen.ttf");
title.setTypeface(music_font);
content.setTypeface(scribble_card);
title.setText(note.getTitle());
date.setText(note.getDateTimeFormatted(getContext()));
if(note.getContent().length() > 25) {
content.setText(note.getContent().substring(0,25) + "...");
} else {
content.setText(note.getContent());
}
if(note.getContent().length() <= 0) {
content.setText("(Empty Note..)");
} else {
content.setText(note.getContent());
}
if (note.getTitle().length() <= 0) {
title.setText("(Untitled)");
} else {
title.setText(note.getTitle());
}
}
return convertView;
}
这是我的笔记课
public class Note implements Serializable {
private long mDateTime;
private String mTitle;
private String mContent;
public Note(long dateTime, String title, String content) {
mContent = content;
mTitle = title;
mDateTime = dateTime;
}
public void setDateTime(long dateTime) {
mDateTime = dateTime;
}
public void setTitle(String title) {
mTitle = title;
}
public void setContent(String content) {
mContent = content;
}
public long getDateTime() {
return mDateTime;
}
public String getTitle() {
return mTitle;
}
public String getContent() {
return mContent;
}
public String getDateTimeFormatted(Context context) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:SS", context.getResources().getConfiguration().locale);
sdf.setTimeZone(TimeZone.getDefault());
return sdf.format(new Date(mDateTime));
}
# 1 楼答案
当您将
SpannableString
转换为正常的String
(如Note.mContent
)时,跨度的信息将丢失。SpannableString使用的跨距与HTML标记完全不同。可以将它们视为实际字符串的附加信息让我们来看看这个字符串:
在这里使用的降价中,看起来是这样的:
Hi, I am a **bold** string.
在HTML中看起来是这样的:
Hi, I am a <strong>bold</strong> string.
然而,作为
SpannableString
是这样的结构:Text : Hi, I am a bold string. Span : Render characters [11..14] in bold
所以有实际文本和附加信息,文本的哪些部分要用粗体显示
如果您想保留这些信息,可以使用Android提供的基本HTML支持,并使用类似
"Hi, I am a <b>bold</b> string."
的字符串,或者必须将格式信息另外保存到文本内容中编辑
要在HTML中使用字符串,必须使用Html.fromHtml():
保存格式信息意味着,必须扩展
Note
类来存储格式信息,基本上是应用于字符串的跨度。例如,您可以使用以下内容:然后,在保存便笺时,必须转换应用于FormatInfo对象的跨距,并将其添加到FormattedNote中。再次创建SpannableString时,必须从便笺的FormatInfo中创建跨距