Android的:如何更新创建控件文本 [英] Android: How to update widget text on creation

查看:285
本文介绍了Android的:如何更新创建控件文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了有关如何更新部件,但没有帮助我与我的问题,几个问题。

我创建了,我想在窗口小部件创建动态更新TextView的一个小部件。 我有添加在屏幕上的窗口小部件时调用的结构的活性。我存储在共享preferences的价值和retreive它的onupdate。的问题是,插件没有更新。任何想法如何可以做到?我不希望以后在小窗口点击更新TextView的,只是得到正确的文本要在创建显示出来。

 公共类AndroidWidget扩展AppWidgetProvider {

@覆盖

公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,
        INT [] appWidgetIds){

    组件名thisWidget =新的组件名(背景下,
            AndroidWidget.class);

    INT [] allWidgetInstancesIds = appWidgetManager
            .getAppWidgetIds(thisWidget);
    对于(INT为widgetid:allWidgetInstancesIds){
        RemoteViews remoteViews =新RemoteViews(context.getPackageName()
                R.layout.widget_layout);

        //创建一个意图接收时将推出PopUpActivity
        意向意图=新的意图(背景下,AndroidWidget.class);
        intent.setAction(SHOW_POPUP_DIALOG_ACTION);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,为widgetid);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(背景下,
                为widgetid,意向,0);

        //设置控件的onClickListener

        remoteViews.setOnClickPendingIntent(R.id.myText,pendingIntent);

        共享preferences preFS = context.getShared preferences(
                将String.valueOf(为widgetid),Context.MODE_PRIVATE);

        remoteViews.setTextViewText(R.id.myText,
                prefs.getString(storedtext,NULL));

        appWidgetManager.updateAppWidget(为widgetid,remoteViews);

    }

    super.onUpdate(背景下,appWidgetManager,appWidgetIds);

}
 

但点击或创建另一个窗口小部件后,仅这实际上更新的TextView。

解决方案

  @覆盖
    公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){

        //设置控件的ID
        组件名thisWidget =新的组件名(背景下,WidgetProvider.class);
        INT [] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        的for(int i = 0; I< appWidgetIds.length;我++){
            意图int​​entService =新的意图(背景下,WidgetService.class);

            intentService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetIds [I]);
            intentService.setData(Uri.parse(intentService.toUri(Intent.URI_INTENT_SCHEME)));

            RemoteViews部件=新RemoteViews(context.getPackageName(),R.layout.widget);

            意图clickIntent =新的意图(背景下,MainActivity.class);
            PendingIntent clickPI = PendingIntent.getActivity(上下文,0,clickIntent,PendingIntent.FLAG_UPDATE_CURRENT);

            widget.setTextViewText(R.id.currency,元);

            appWidgetManager.updateAppWidget(appWidgetIds [I]中,插件);
        }
        super.onUpdate(背景下,appWidgetManager,appWidgetIds);

    }
 

这code对我的作品。你可以试试这个。

I have seen several questions regarding how to update widgets but nothing is helping me with my issue.

I created a widget that has a textview that I want to dynamically update upon widget creation. I have a configuration activity that is called when adding the widget on the screen. I store the value in the sharedpreferences and retreive it onUpdate. The problem is that the widget doesn't get updated. Any idea how it can be done? I do not want to update the textview after a click on the widget, just get the correct text to be displayed on creation.

public class AndroidWidget extends AppWidgetProvider {

@Override

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    ComponentName thisWidget = new ComponentName(context,
            AndroidWidget.class);

    int[] allWidgetInstancesIds = appWidgetManager
            .getAppWidgetIds(thisWidget);
    for (int widgetId : allWidgetInstancesIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget_layout);

        // Create an intent that when received will launch the PopUpActivity
        Intent intent = new Intent(context, AndroidWidget.class);
        intent.setAction(SHOW_POPUP_DIALOG_ACTION);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                widgetId, intent, 0);

        // Set up the onClickListener of the widget

        remoteViews.setOnClickPendingIntent(R.id.myText, pendingIntent);

        SharedPreferences prefs = context.getSharedPreferences(
                String.valueOf(widgetId), Context.MODE_PRIVATE);

        remoteViews.setTextViewText(R.id.myText,
                prefs.getString("storedtext", null));

        appWidgetManager.updateAppWidget(widgetId, remoteViews);

    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);

}

This actually updates the textview but only after clicking or creating another widget.

解决方案

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        //set widget id
        ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        for (int i = 0; i < appWidgetIds.length; i++) {
            Intent intentService = new Intent(context, WidgetService.class);

            intentService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
            intentService.setData(Uri.parse(intentService.toUri(Intent.URI_INTENT_SCHEME)));

            RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);

            Intent clickIntent = new Intent(context, MainActivity.class);
            PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            widget.setTextViewText(R.id.currency, " Dollar");

            appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);

    }

This code works for me. You may try this.

这篇关于Android的:如何更新创建控件文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆