我正在尝试创建一个基本的文本编辑器。我几乎实现了我想做的事情,但是存在一些问题。当我按“ B”按钮时,它使文本变为粗体。如果我在不留空格的情况下继续书写,然后再次按“ B”按钮,则我写的所有字母都会恢复正常。但是我想使字母已经加粗。当我提供空间时,它会按我的意愿工作,使字体大胆,斜体或正常。这是我的代码的一部分:
editor.textChangedListener {
beforeTextChanged { _, _, _, _ ->
val length = editor.text?.length
length?.let {
if (it > 0) {
val styleSpan = when {
isBold && isItalic -> StyleSpan(Typeface.BOLD_ITALIC)
isBold && !isItalic -> StyleSpan(Typeface.BOLD)
!isBold && isItalic -> StyleSpan(Typeface.ITALIC)
else -> StyleSpan(Typeface.NORMAL)
}
val spannable = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
if (it < start) start = 0
editor.text?.setSpan(styleSpan, start, it, spannable)
}
}
}
}
boldButton.onClick {
isBold = !isBold
val image = when (isBold) {
true -> R.drawable.ic_bold_active
false -> R.drawable.ic_bold
}
start = editor.text!!.length
boldButton.setImageResource(image)
}
答案 0 :(得分:0)
要更改字体类型,您可以使用类似这样的东西。
public ResponseEntity<String> getResponseEntityDemo(){
return new ResponseEntity<>("This is the body", HttpStatus.OK);
}
您也可以尝试使用html
TextView tv = (TextView) findViewById(R.id.tvname);
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/epimodem.ttf");
tv.setTypeface(face);
答案 1 :(得分:0)
单击粗体或斜体按钮时,更改字体时可以更新。
在代码中添加以下方法
fun onUpdateTypeface() {
when {
isBold && isItalic ->
view?.tvForgetPassword?.setTypeface(null, Typeface.BOLD_ITALIC);
isBold && !isItalic ->
view?.tvForgetPassword?.setTypeface(null, Typeface.BOLD);
!isBold && isItalic ->
view?.tvForgetPassword?.setTypeface(null, Typeface.ITALIC);
else -> view?.tvForgetPassword?.setTypeface(null, Typeface.NORMAL);
}
}
更新您的onClick方法
boldButton.onClick {
isBold = !isBold
val image = when (isBold) {
true -> R.drawable.ic_bold_active
false -> R.drawable.ic_bold
}
start = editor.text!!.length
boldButton.setImageResource(image)
onUpdateTypeface()
}