How do I append text to a TextView in a widget?

时间:2017-04-10 00:29:54

标签: android android-widget

I'm writing a simple android widget containing only a TextView. This is my AppWidgetProvider code:

public class EventsWidgetProvider extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int widgetId : appWidgetIds) {
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.events_widget);

            remoteViews.setTextViewText(R.id.textView, "foo");

            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
    }
}

This simply uses RemoteViews.setTextViewText to set the text view text to "foo", and works.

But there is no RemoteViews.appendTextViewText. What would I do if I want to append "bar" to the text view?

1 个答案:

答案 0 :(得分:0)

It turns out you literally can't. While this works:

remoteViews.setCharSequence(R.id.textView, "setText", charSequence);

This throws an error.

remoteViews.setCharSequence(R.id.textView, "append", charSequence);

Error: android.widget.RemoteViews$ActionException: view: android.widget.TextView can't use method with RemoteViews: append(interface java.lang.CharSequence)

Edit: According to this answer you can only use methods with the @RemotableViewMethod annotation.