【问题标题】:Android setTextViewText not workingAndroid setTextViewText 不起作用
【发布时间】:2013-06-03 14:21:58
【问题描述】:

好的,所以我有一个 android 小部件,但我在更新 android 小部件时遇到了问题。我使用了一些简单的硬编码setTextViewText,但是当我调试应用程序时没有任何反应..

我的代码:

public class Widget extends AppWidgetProvider {

RemoteViews views;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    int N = appWidgetIds.length;
    views = new RemoteViews(context.getPackageName(), R.layout.widget);
    for (int i = 0; i < N; i++) {
        int appWidgetId = appWidgetIds[i];
        String alarm = Settings.System.getString(context.getContentResolver(),
            Settings.System.NEXT_ALARM_FORMATTED);
        views.setTextViewText(R.id.tvAlarm, alarm);

        views.setTextViewText(R.id.tvNextAlarm, "qwertyuiop");

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

我知道一些代码是错误的,例如警报,我正试图以某种方式合并电池状态,但我被告知 BroadcastReceiver 不是前进的方向,所以目前我已经注释掉了。

但主要问题是更新小部件以更改 TextView 不起作用...

【问题讨论】:

    标签: android android-widget widget broadcastreceiver updates


    【解决方案1】:

    我刚刚在我的一个小部件中尝试了这段代码,它运行良好:

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    
        // Get ids of all the instances of the widget
        ComponentName widget = new ComponentName(context, MediaWidgerProvider.class);
        int[] widgetIds = appWidgetManager.getAppWidgetIds(widget);
    
        for (int widgetId : widgetIds) {
    
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.my_widget);
    
            remoteViews.setTextViewText(R.id.widget_text, "Hello");
    
            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-24
      • 2010-10-28
      • 2016-05-26
      • 2016-06-19
      • 2015-04-21
      • 2018-10-15
      • 2011-04-03
      • 2016-12-03
      相关资源
      最近更新 更多