简体   繁体   English

将clickCount扩展到2个以上的项目

[英]Extend clickCount to more than 2 items

I am learning to create Android widget following the tutorial from https://looksok.wordpress.com/2012/12/15/android-complete-widget-tutorial-including-source-code/ 我正在按照https://looksok.wordpress.com/2012/12/15/android-complete-widget-tutorial-includes-source-code/中的教程学习如何创建Android小部件

Tutorial showed how to build android widget with one button and image. 教程显示了如何使用一个按钮和图像构建android小部件。 Button press will change the image displayed. 按下按钮将改变显示的图像。

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;
}

} }

What I want to do is to extend the clickCount to multiple (12 images). 我要做的是将clickCount扩展为多个(12张图片)。 The author commented that: Put drawables in an ArrayList and after button click get the drawable corresponding to clickCount Remember to reset counter if it reaches ArrayList's size to avoid IndexOutOfBounds exception 作者评论说:将可绘制对象放入ArrayList中,并在单击按钮后获取与clickCount相对应的可绘制对象。记住,如果计数器达到ArrayList的大小,则要重置计数器,以避免IndexOutOfBounds异常

But I really have no idea on how to do this since I have only beginning knowledge in Java and Android development. 但是我真的不知道如何执行此操作,因为我只有Java和Android开发方面的入门知识。

This is how I made it: 这是我的方法:

public class MyWidgetIntentReceiver extends BroadcastReceiver {

private static int clickCount = 0;

@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("com.example.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++;
    if (clickCount == 1) {
        return R.drawable.image1 ;
    }
    if (clickCount == 2) {
        return R.drawable.image2 ;
    }
    if (clickCount == 3) {
        return R.drawable.image3 ;
    }
    clickCount = 0;
    return R.drawable.image4 ;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM