【问题标题】:Display Widget ImageView based on month基于月份显示 Widget ImageView
【发布时间】:2015-04-02 14:55:41
【问题描述】:

我有 12 张图片保存在 drawable 中,文件名按月份命名,即 jan、feb、mar、apr 等。如何根据当前月份在 Android 小部件的 ImageView 中显示图像?例如,当前是四月,所以应该显示文件名为 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;
}

} 我应该做哪些修改?我不需要按钮。我只想按月份显示图像。

【问题讨论】:

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


    【解决方案1】:

    使用Calendar自己解决了。

    【讨论】:

    • 最好使用 switch-case 或 const 数组中的索引
    猜你喜欢
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    相关资源
    最近更新 更多