如何在扩展 appwidget 提供程序时在 android 中随机设置图像视图资源
How Do I set an Image view resource randomly in android while extending the appwidget provider
您好,我是 android 编程的新手,我需要一些帮助来设置我的小部件,我一直在尝试在小部件更新时随机设置图像视图资源,我已经尝试过了
Random r = new Random();
int min = 001;
int max = 721;
String name = "national" + r.nextInt(max - min + 1) + min;
int id = getResources().getIdentifier(name, "drawable", getPackageName());
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.source);
views.setOnClickPendingIntent(R.id.imgwidg, pendingIntent);
views.setImageViewResource(id);
但它不接受 getResources()
或 getIdentifier()
我该怎么办?
编辑:
我也很确定它不会喜欢 001 那么我如何将数字格式化为 3 位数字?
编辑 2:(已解决)感谢@ci_ 和@blipinsk
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context,appWidgetManager,appWidgetIds);
final int N = appWidgetIds.length;
Log.i("ExampleWidget", "Updating widgets " + Arrays.asList(appWidgetIds));
// Perform this loop procedure for each App Widget that belongs to this
// provider
for (int i = 0; i < appWidgetIds.length; i++) {
Random r = new Random();
int min = 1;
int max = 721;
int id3 = r.nextInt(max)+min;
int id2 = id3 - 1;
Resources res = context.getResources();
String[] title = res.getStringArray(R.array.nationaltitles);
String name = "national" + String.format("%03d",id3);
int id = context.getResources().getIdentifier(name, "drawable", context.getPackageName());
if (id == id){
id = context.getResources().getIdentifier(name, "drawable", context.getPackageName());
id2 = id3 - 1;
}
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, SplashActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.source);
views.setOnClickPendingIntent(R.id.imgwidg, pendingIntent);
views.setImageViewResource(R.id.imgwidg, id);
views.setTextViewText(R.id.tv2,title[id2]);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
getResources()
是Context
的一个方法。所有 AppWidgetProvider
methods 都提供了一个 Context
作为参数,因此您可以使用它来调用 getResources()
on.
要将整数格式化为特定宽度,您可以使用 [String.format()](http://developer.android.com/reference/java/lang/String.html#format(java.lang.String, java.lang.Object...))
即String.format("%03d", r.nextInt(max - min + 1) + min)
请务必将代码的开头更改为:
int min = 1;
int max = 721;
String name = "national" + String.format("%03d", r.nextInt(max - min + 1) + min);
这将格式化您的整数以匹配 XYZ
格式(如果需要,三位数带前导零)。
您好,我是 android 编程的新手,我需要一些帮助来设置我的小部件,我一直在尝试在小部件更新时随机设置图像视图资源,我已经尝试过了
Random r = new Random();
int min = 001;
int max = 721;
String name = "national" + r.nextInt(max - min + 1) + min;
int id = getResources().getIdentifier(name, "drawable", getPackageName());
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.source);
views.setOnClickPendingIntent(R.id.imgwidg, pendingIntent);
views.setImageViewResource(id);
但它不接受 getResources()
或 getIdentifier()
我该怎么办?
编辑: 我也很确定它不会喜欢 001 那么我如何将数字格式化为 3 位数字?
编辑 2:(已解决)感谢@ci_ 和@blipinsk
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context,appWidgetManager,appWidgetIds);
final int N = appWidgetIds.length;
Log.i("ExampleWidget", "Updating widgets " + Arrays.asList(appWidgetIds));
// Perform this loop procedure for each App Widget that belongs to this
// provider
for (int i = 0; i < appWidgetIds.length; i++) {
Random r = new Random();
int min = 1;
int max = 721;
int id3 = r.nextInt(max)+min;
int id2 = id3 - 1;
Resources res = context.getResources();
String[] title = res.getStringArray(R.array.nationaltitles);
String name = "national" + String.format("%03d",id3);
int id = context.getResources().getIdentifier(name, "drawable", context.getPackageName());
if (id == id){
id = context.getResources().getIdentifier(name, "drawable", context.getPackageName());
id2 = id3 - 1;
}
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, SplashActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.source);
views.setOnClickPendingIntent(R.id.imgwidg, pendingIntent);
views.setImageViewResource(R.id.imgwidg, id);
views.setTextViewText(R.id.tv2,title[id2]);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
getResources()
是Context
的一个方法。所有 AppWidgetProvider
methods 都提供了一个 Context
作为参数,因此您可以使用它来调用 getResources()
on.
要将整数格式化为特定宽度,您可以使用 [String.format()](http://developer.android.com/reference/java/lang/String.html#format(java.lang.String, java.lang.Object...))
即String.format("%03d", r.nextInt(max - min + 1) + min)
请务必将代码的开头更改为:
int min = 1;
int max = 721;
String name = "national" + String.format("%03d", r.nextInt(max - min + 1) + min);
这将格式化您的整数以匹配 XYZ
格式(如果需要,三位数带前导零)。