我想将选定的文本设置为 EditText
粗体。
已经可以找出我选择了哪些字符,并且使用 getSelectionStart() 和 getSelectionEnd( )
我知道位置在哪里。
但我的问题是,我想用按钮将选定的文本设置为粗体,但不知道如何用字符串设置字体粗体(我只知道如何用 EditText
).
所以这是我的示例代码:
fettdruckButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectionStart = fullscreenEdittext.getSelectionStart();
int selectionEnd = fullscreenEdittext.getSelectionEnd();
int differenz = selectionEnd - selectionStart;
String selectedText = fullscreenEdittext.getText().toString().substring(selectionStart, selectionEnd);
Log.d("selectedText", "selectedText " + selectedText + "|" + selectionStart + "|" + selectionEnd);
//fullscreenEdittext.setTypeface(null, Typeface.BOLD);
}
});
最佳答案
使用 SpannableStringBuilder。
SpannableStringBuilder stringBuilder = (SpannableStringBuilder) fullscreeneditText.getText();
stringBuilder.setSpan(new StyleSpan(Typeface.BOLD), selectionStart, selectionEnd, 0);
请注意,如果您想多次执行此操作,您应该先删除原始跨度。
关于android - 如何设置选中的文本粗体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25601741/