在Android小部件中浏览文本的子字符串

时间:2016-03-02 20:11:28

标签: android textview widget android-widget strikethrough

我的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);

问题是文本视图中的所有文本都会出现这种情况。如何仅浏览部分文本视图的文本?

2 个答案:

答案 0 :(得分:3)

Xoce的答案非常正确,但它更适合应用程序文本视图中的一般答案,但通过一些调整,我可以为小部件做到这一点。 (也感谢CommonsWare推动我朝着正确的方向发展。

在updateAppWidget方法中,您可以使用远程视图向textview添加文本。要通过删除来自定义textview文本的子字符串,请使用spannable字符串构建器(您还可以使用不同的跨度来实现粗体,下划线,斜体等)。

这就是我的所作所为:

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);

    ...

}

答案 1 :(得分:2)

使用SpannableStringBuilderStrikethroughSpan

例如,要获得以下效果,请参阅下面的代码段: enter image description here

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);

更酷的效果 here