在 Android 小部件中删除文本的子字符串
Strike through substring of text in Android widget
我的 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);
问题是这划掉了文本视图中的所有文本。如何只删除文本视图文本的一部分?
使用SpannableStringBuilder and StrikethroughSpan
例如,要获得以下效果,请参见下面的代码片段:
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
Xoce 的答案非常正确,但它更像是应用程序文本视图中的一般答案,但通过一些调整,我可以为小部件做到这一点。 (也感谢 CommonsWare 将我推向正确的方向。
在updateAppWidget 方法中,您可以使用远程视图向textview 添加文本。要自定义带删除线的 textview 文本的子字符串,请使用 spannable string builder(您还可以使用不同的 span 来实现粗体、下划线、斜体等)
这是我所做的:
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);
...
}
我的 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);
问题是这划掉了文本视图中的所有文本。如何只删除文本视图文本的一部分?
使用SpannableStringBuilder and StrikethroughSpan
例如,要获得以下效果,请参见下面的代码片段:
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
Xoce 的答案非常正确,但它更像是应用程序文本视图中的一般答案,但通过一些调整,我可以为小部件做到这一点。 (也感谢 CommonsWare 将我推向正确的方向。
在updateAppWidget 方法中,您可以使用远程视图向textview 添加文本。要自定义带删除线的 textview 文本的子字符串,请使用 spannable string builder(您还可以使用不同的 span 来实现粗体、下划线、斜体等)
这是我所做的:
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);
...
}