我有这样的方法,它可以很好地应用于EditText或视图。
public SpannableString strikeThrough(String txt){
SpannableString spannableString = new SpannableString(txt);
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
spannableString.setSpan(strikethroughSpan,0, txt.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannableString;
}
EditText etChecklistItem = checklistView.findViewById(R.id.et_checklist_item);
etChecklistItem.setText(strikeThrough(etChecklistItem.getText().toString()));但我的问题是,在那之后,文本没有StrikeThrough。
StringBuilder stringBuilderItem = new StringBuilder();
for( String list : itemslist) {
stringBuilderItem.append(list);
stringBuilderItem.append("\n");
}
String text = stringBuilderItem.toString();
strikeThrough(text);
dbHelper.insertChecklist(text);当我在RecyclerView中得到数据时,它不会被删除。
发布于 2022-06-28 13:11:24
与其在数组中存储一个通过罢工的字符串,不如创建一个具有两个成员--一个class和一个String --的boolean,并为您想要删除的字符串创建一个布尔true。
class Message {
private String str;
private boolean strike;
public Message (String str, boolean strike) {
this.str = str;
this.strike = strike;
}
// getters and setters
}当你在屏幕上显示它的时候,让字符串划过
ArrayList<Message> arr = new ArrayList<>();
for (Message msg: arr) {
if (arr.getStrike()) {
// make string strikethrough
} else {
// keep as it is
}
}在TextView中敲击字符串
textView.setText("I want like that")
textView.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);String str = "I want like that";
SpannableStringBuilder builder = new SpannableStringBuilder(str);
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
builder.setSpan(
strikethroughSpan,
0, // Start
4, // End (exclusive)
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE // Text changes will not reflect in the strike changing
);
textView.setText(spanBuilder);<string name="yourName"><strike>I want like that</strike></string>参考资料:[1]
发布于 2022-06-28 13:08:00
只要检查一下这个代码
首先,您需要初始化编辑文本或文本视图。
textView = findViewById(R.id.textView);
textView.setText("Hello World");
textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);我希望这会对你有所帮助,如果有什么问题,请评论一下。
https://stackoverflow.com/questions/72786519
复制相似问题