我正在使用编辑文本来支持粗体,斜体和下划线的属性。在选择文本并使其变为粗体后,我获得了成功。现在我想在点击Normal按钮后删除粗体。
Typeface.NORMAL 在此处无效。任何人都可以建议其他选择。
Button btnBold = (Button) findViewById(R.id.btnBold);
btnBold.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startSelection = etx.getSelectionStart();
endSelection = etx.getSelectionEnd();
Spannable s = etx.getText();
s.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startSelection, endSelection, 0);
}
});
Button btnNormal = (Button) findViewById(R.id.btnNormal );
btnNormal .setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
**//What I have to do here.**
}
});
答案 0 :(得分:7)
Button btnNormal = (Button) findViewById(R.id.btnNormal );
btnNormal .setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Spannable str = etx.getText();
StyleSpan[] ss = str.getSpans(selectionStart, selectionEnd, StyleSpan.class);
for (int i = 0; i < ss.length; i++) {
if (ss[i].getStyle() == android.graphics.Typeface.BOLD){
str.removeSpan(ss[i]);
}
}
etx.setText(str);
}
});
答案 1 :(得分:0)
在我的项目中,我使用这种结构
textView.typeface = Typeface.create(textView.typeface, Typeface.NORMAL)
答案 2 :(得分:-1)
答案 3 :(得分:-1)
与您在第一个onClick()
中使用的内容类似,而不是s.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startSelection, endSelection, 0);
在第二个s.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), startSelection, endSelection, 0);
中使用onclick()
。