5

所以在我的 AppWidgetProvider 类的 onUpdate 方法中,我最终执行了大量的代码,这样我就可以完全重新创建一个新的 RemoteViews 对象。现实情况是,我真的只需要在每次更新时在 RemoteViews 中的一个文本视图中设置文本。无论如何只修改特定小部件已经使用的 RemoteViews 吗?

-库蒂斯

4

3 回答 3

18

首先,RemoteView 不是视图。它是一组构建视图层次结构的指令。它用于在另一个进程中重新创建视图(App Widgets 不会在您的应用进程中执行)。因此它是可序列化和可变的。

因此,当您初始化 RemoteView 时,只需在某处存储对它的引用,例如在您的自定义 AppWidgetProvider 的字段中。下次您需要它时,从现场获取它并对其进行更改。要更改 TextView 中的字符串,请使用setString(..).

remoteView.setString(textViewId, "setText", "some text for TextView")
于 2011-10-11T20:45:16.970 回答
17

如果您使用当前的 API,这是 2013 年的更新。在将执行更新的 WidgetProvider 类的方法中:

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
rv = new RemoteViews(context.getPackageName(), R.layout.widgetlayout); 
rv.setTextViewText(R.id.ofTextViewInWidgetLayoutXML, "Hello World");
appWidgetManager.partiallyUpdateAppWidget(appWidgetIds[i], rv);

请注意,它不再是 remoteView.setString 而是remoteView.setTextViewText

于 2013-08-28T13:20:23.380 回答
0

您可以更新远程视图,然后调用

ComponentName componentName= new ComponentName(context, YourClass.class);
AppWidgetManager.getInstance(context).updateAppWidget(componentName, remoteViews);

on,AFAIK 应该更新小部件

于 2010-12-25T01:22:45.957 回答