Android setTextViewText 不起作用

标签 android android-widget widget broadcastreceiver updates

好的,我有一个 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 不起作用...

最佳答案

我刚刚在我的一个小部件中尝试了这段代码,它工作正常:

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

关于Android setTextViewText 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16981355/

相关文章:

java - 如何从 OnCreate 方法外部更改小部件的可见性? Java Android Studio

Android如何在软键盘可见时在全屏模式下调整布局

android - findViewById 未定义

android - 有没有办法让 UI 元素稍微重叠(一个在另一个之上)而不使用绝对布局?

R : combine and save rgl widgets as a single html file

Android MapView 与 DrawerLayout 重叠

android - 如何将 android studio 项目从 linux 移植到 windows

android - 如何根据一些特定的分辨率计算分辨率?

dart - 多个小部件与 TabController 使用相同的 GlobalKey

android - Robolectric : NotFoundException: unknown resource 0 at org. robolectric.shadows.ShadowAssetManager.getAndResolve (ShadowAssetManager.java:258)