如何在android中验证文本样式
我正在用android开发一个简单的文本编辑器,它几乎没有什么基本选项,比如粗体、斜体、粗体+斜体、下划线划线。下面是我用来在单击相应按钮时将效果应用于所选文本的代码如何在android中验证文本样式,android,android-edittext,text-editor,rich-text-editor,Android,Android Edittext,Text Editor,Rich Text Editor,我正在用android开发一个简单的文本编辑器,它几乎没有什么基本选项,比如粗体、斜体、粗体+斜体、下划线划线。下面是我用来在单击相应按钮时将效果应用于所选文本的代码 public void buttonClick(View target){ Log.i("Information ", "In click"); View parentView = (View) target.getParent(); final EditText editorField = (EditT
public void buttonClick(View target){
Log.i("Information ", "In click");
View parentView = (View) target.getParent();
final EditText editorField = (EditText) parentView.findViewById(R.id.editor_field);
int startSelection = editorField.getSelectionStart();
int endSelection = editorField.getSelectionEnd();
Spannable textEdit = editorField.getText();
if(target.getId() == R.id.boldButton){
textEdit.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startSelection, endSelection, 0);
} else if(target.getId() == R.id.italicButton){
textEdit.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), startSelection, endSelection, 0);
} else if(target.getId() == R.id.underlineButton){
textEdit.setSpan(new UnderlineSpan(), startSelection, endSelection, Spannable.SPAN_MARK_MARK);
} else if(target.getId() == R.id.strikeButton){
textEdit.setSpan(new StrikethroughSpan(), startSelection, endSelection, Spannable.SPAN_MARK_MARK);
} else if(target.getId() == R.id.boldItalicButton){
textEdit.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), startSelection, endSelection, 0);
}
}
但在应用文本效果之前,我必须检查所选文本是否已经具有任何文本效果。i、 e.最初所选文本为粗体,因此当单击粗体按钮时,必须删除该文本效果。我不知道该怎么做。谁能帮我一下吗
注意。您可以通过调用getSpans()方法获得样式列表: 然后使用StyleSpan类的方法:
if (styleSpans[i].getStyle() == android.graphics.Typeface.BOLD){
...
}
if (styleSpans[i].getStyle() == android.graphics.Typeface.BOLD){
...
}