根据月份显示小部件ImageView

时间:2015-04-02 14:55:41

标签: android android-widget android-imageview android-appwidget

我有12个图像保存在drawable中,文件名根据月份命名,即jan,feb,mar,apr等。如何根据当前月份在Android小部件的ImageView中显示图像?例如,目前是4月,因此应显示文件名为apr的图像。 我正在关注https://looksok.wordpress.com/2012/12/15/android-complete-widget-tutorial-including-source-code/

的教程
public class MyWidgetIntentReceiver extends BroadcastReceiver {

private static int clickCount = 0;

@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("pl.looksok.intent.action.CHANGE_PICTURE")){
        updateWidgetPictureAndButtonListener(context);
    }
}

private void updateWidgetPictureAndButtonListener(Context context) {
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_demo);
    remoteViews.setImageViewResource(R.id.widget_image, getImageToSet());

    //REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!!
    remoteViews.setOnClickPendingIntent(R.id.widget_button, MyWidgetProvider.buildButtonPendingIntent(context));

    MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(), remoteViews);
}

private int getImageToSet() {
    clickCount++;
    return clickCount % 2 == 0 ? R.drawable.me : R.drawable.wordpress_icon;
}

} 我应该做什么修改?我不需要按钮。我只想根据月份显示图像。

1 个答案:

答案 0 :(得分:0)

使用Calendar自行解决。