ユーザーがボタンを押したときに、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);
}
}
}
上記のコードを使用して、特定の位置に箇条書きと番号を追加できますが、問題は、Enterキーを押すと次の行に自動的に箇条書きを追加し、テキストを入力せずにEnterキーを押すと、次のような機能を実装したいことです2行目にテキストを入力してEnterキーを押すと、その箇条書きを削除する必要があります。次の行にも箇条書きを追加する必要があります。このアプリケーションと同様の機能。 https://play.google.com/store/apps/details?id=com.hly.notes
番号リストでも同じですが、常に追加されます1)値は増分されません。また、番号リストについても同様の機能の王様を実装したいと思います。番号リストを適用してEnterキーを押すと、自動的に2が追加されます)次の行へ。
誰でもこれを達成する方法を知っています。前もって感謝します。