2

Android ウィジェットにテキスト ビューがあり、特定のテキスト行のみに取り消し線を引く必要があります。ウィジェット内のテキストに取り消し線を引くために、別の SO の質問でこれを見つけました。

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);

// strike through text, this strikes through all text
views.setInt(R.id.appwidget_text, "setPaintFlags", Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

問題は、これがテキスト ビュー内のすべてのテキストを打ち消すことです。テキスト ビューのテキストの一部だけに取り消し線を付けるにはどうすればよいですか?

4

2 に答える 2

5

SpannableStringBuilderStrikethroughSpanを使用する

たとえば、次の効果を得るには、以下のスニペットを参照してください。 ここに画像の説明を入力


String firstWord = "Hello";
String secondWord = "World!";

TextView tvHelloWorld = (TextView)findViewById(R.id.tvHelloWorld);

// Create a span that will make the text red
ForegroundColorSpan redForegroundColorSpan = new ForegroundColorSpan(
    getResources().getColor(android.R.color.holo_red_dark));

// Use a SpannableStringBuilder so that both the text and the spans are mutable
SpannableStringBuilder ssb = new SpannableStringBuilder(firstWord);

// Apply the color span
ssb.setSpan(
    redForegroundColorSpan,            // the span to add
    0,                                 // the start of the span (inclusive)
    ssb.length(),                      // the end of the span (exclusive)
    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // behavior when text is later inserted into the SpannableStringBuilder
                                       // SPAN_EXCLUSIVE_EXCLUSIVE means to not extend the span when additional
                                       // text is added in later

// Add a blank space
ssb.append(" ");

// Create a span that will strikethrough the text
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();

// Add the secondWord and apply the strikethrough span to only the second word
ssb.append(secondWord);
ssb.setSpan(
    strikethroughSpan,
    ssb.length() - secondWord.length(),
    ssb.length(),
    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

// Set the TextView text and denote that it is Editable
// since it's a SpannableStringBuilder
tvHelloWorld.setText(ssb, TextView.BufferType.EDITABLE);

ここでよりクールな効果

于 2016-03-02T20:28:11.893 に答える
3

Xoceの答えはかなり正しいですが、アプリ内のテキストビューに対する一般的な答えでしたが、微調整を加えてウィジェットに対して行う方法があります。(また、私を正しい方向に押し進めてくれた CommonsWare にも感謝します。

updateAppWidget メソッドでは、リモート ビューを使用して textview にテキストを追加できます。テキストビューのテキストの部分文字列を取り消し線でカスタマイズするには、スパン可能な文字列ビルダーを使用します (別のスパンを使用して太字、下線、斜体などを実現することもできます)。

これが私がしたことです:

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {
    CharSequence widgetText = NewAppWidgetConfigureActivity.loadTitlePref(context, appWidgetId, NewAppWidgetConfigureActivity.TEXT_KEY);
    // Construct the RemoteViews object
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);

    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(widgetText);
    StrikethroughSpan strikethroughSpan = new StrikethroughSpan();

    spannableStringBuilder.setSpan(strikethroughSpan, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    views.setTextViewText(R.id.appwidget_text, spannableStringBuilder);

    ...

}
于 2016-03-02T23:22:53.560 に答える