Android 当我从编辑文本中删除字体时,应用程序崩溃

Android 当我从编辑文本中删除字体时,应用程序崩溃,android,android-edittext,Android,Android Edittext,如何从edittext中删除粗体样式,同时保留其他格式。当我尝试将字体设置为正常时,我的应用程序崩溃。将字体设置为粗体不是问题 int startSelection = _etheadertext.getSelectionStart(); int endSelection = _etheadertext.getSelectionEnd(); if (startSelection > endSelection) { startSelection = _ethe

如何从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();

        }

    }

尝试在
strings.xml
文件夹中使用
Bold

根据代码,您可以这样做:

String sourceString=”“+id+“”+name
mytextview.setText(Html.fromHtml(sourceString))

或:


大胆的正常的
]]>

并将其命名为:
textView.setText(Html.fromHtml(getString(R.string.test))

Logcat请分享。请出示Logcat您能详细解释一下吗?我只想突出显示文本并将其加粗(edittext应该加粗,而不仅仅是突出显示的文本),然后再次按下使其未绑定,如MsWord@Sarah检查我的更新答案,以获得加粗的选项。那么我们如何从文本中删除加粗并保留其他格式呢。还有,我们以后如何检查特定字符串中是否有粗体字母
<string name="test">
     <![CDATA[
 <b>bold!</b> normal
    ]]>
</string>