简体   繁体   English

如何在启用时更新小部件布局?

[英]how to update the widget layout on enable?

I'm creating a three widgets for my application , when I drag the three widgets to the main screen , I get the same widget layout but on update the widgets layout will change , 我正在为我的应用程序创建三个小部件,将这三个小部件拖到主屏幕时,我得到的是相同的小部件布局,但是在更新时,这些小部件布局会更改,

How can i update the three widgets layouts immediately once dragged to the main screen ? 拖动到主屏幕后,如何立即更新三个小部件的布局?

Here is my code : 这是我的代码:

public class FullWidgetProvider extends AppWidgetProvider {


    private int widgetID;
    private static SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy  hh:mm:ss a");
    static String strWidgetText = "";



    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // TODO Auto-generated method stub
        //super.onDeleted(context, appWidgetIds);
    //  Toast.makeText(context, "onDeleted()", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDisabled(Context context) {
        // TODO Auto-generated method stub
        //super.onDisabled(context);
        //Toast.makeText(context, "onDisabled()", Toast.LENGTH_LONG).show();
    }



    @Override
    public void onEnabled(Context context) {
        // TODO Auto-generated method stub
        super.onEnabled(context);


    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onUpdate(context, appWidgetManager, appWidgetIds);

        final int N = appWidgetIds.length;
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];
            updateAppWidget(context, appWidgetManager, appWidgetId);

        }

    }

this is part of my code, but you can get an idea 这是我的代码的一部分,但是您可以了解

@Override 
public void onEnabled(Context context) {
    super.onEnabled(context);

    AppWidgetManager appManager = AppWidgetManager.getInstance(context); 
    ComponentName thisWidget = new ComponentName(context, ExampleAppWidgetProvider.class);
    RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget1);
    updateViews.setTextColor(R.id.widget1label, Color.YELLOW);
    appManager.updateAppWidget(thisWidget, updateViews); }

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

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