如何在保留其他格式的同时,从edittext中删除粗体样式。尝试将字体设置为正常时,我的应用程序崩溃。将字体设置为粗体不是问题
int startSelection = _etheadertext.getSelectionStart();
int endSelection = _etheadertext.getSelectionEnd();
if (startSelection > endSelection) {
startSelection = _etheadertext.getSelectionEnd();
endSelection = _etheadertext.getSelectionStart();
}
Spannable s = _etheadertext.getText();
s.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startSelection, endSelection, 0);
_etheadertext.setText(s);
_etheadertext.setSelection(startSelection, endSelection);
if(_etheadertext.hasSelection()) {
try {
if (_etheadertext.getTypeface().getStyle() == Typeface.NORMAL) {
_etheadertext.setTypeface(null, Typeface.BOLD);
Toast.LENGTH_SHORT).show();
chk = false;
}
if ((_etheadertext.getTypeface().getStyle() == Typeface.BOLD) && (chk == true)) {
_etheadertext.setTypeface(null, Typeface.NORMAL);
// this line crashes the app
chk = false;
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:-1)
尝试在Bold
文件夹中使用strings.xml
。
通过代码,您可以这样做:
String sourceString = "<b>" + id + "</b> " + name;
mytextview.setText(Html.fromHtml(sourceString));
或:
<string name="test">
<![CDATA[
<b>bold!</b> normal
]]>
</string>
并像这样调用它:textView.setText(Html.fromHtml(getString(R.string.test)));