当用户按下按钮时,我正在尝试在 android 编辑文本中应用项目符号列表和编号列表。为此,我尝试了以下代码。
对于子弹
BulletSpan[] quoteSpan = str.getSpans(selectionStart, selectionEnd, BulletSpan.class);
// If the selected text-part already has UNDERLINE style on it, then we need to disable it
for (int i = 0; i < quoteSpan.length; i++) {
str.removeSpan(quoteSpan[i]);
exists = true;
}
// Else we set UNDERLINE style on it
if (!exists) {
str.setSpan(new BulletSpan(10), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
break;
和编号
int number = 0;
NumberIndentSpan[] quoteSpan1 = str.getSpans(selectionStart, selectionEnd, NumberIndentSpan.class);
number ++;
// If the selected text-part already has UNDERLINE style on it, then we need to disable it
for (int i = 0; i < quoteSpan1.length; i++) {
str.removeSpan(quoteSpan1[i]);
exists = true;
}
if (!exists){
str.setSpan(new NumberIndentSpan(5, 15, number), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
break;
NumberIndentSpan.java
public class NumberIndentSpan implements LeadingMarginSpan {
private final int gapWidth;
private final int leadWidth;
private final int index;
public NumberIndentSpan(int leadGap, int gapWidth, int index) {
this.leadWidth = leadGap;
this.gapWidth = gapWidth;
this.index = index;
}
public int getLeadingMargin(boolean first) {
return leadWidth + gapWidth;
}
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top,
int baseline, int bottom, CharSequence text, int start, int end,
boolean first, Layout l) {
if (first) {
Paint.Style orgStyle = p.getStyle();
p.setStyle(Paint.Style.FILL);
float width = p.measureText("4.");
c.drawText(index + ")", (leadWidth + x - width / 2) * dir, bottom
- p.descent(), p);
p.setStyle(orgStyle);
}
}
}
通过上面的代码,我可以在特定位置添加项目符号和数字,但问题是,我想实现这样的功能,当我按下回车键时,它应该自动将项目符号添加到下一行,如果我按下回车键而不输入任何文本,它应该自动添加项目符号它应该删除那个子弹,如果我在第二行输入一些文本,然后如果我按下回车键,它应该也将子弹添加到下一行。与此应用程序类似的功能。 https://play.google.com/store/apps/details?id=com.hly.notes
编号列表也一样,它总是添加1)它不会增加值,对于编号列表,我也想实现类似的功能之王,如项目符号,当我应用编号列表时,当我按 Enter 时,它应该自动添加2 )到下一行。
任何人都知道如何实现这一目标。先感谢您。