【问题标题】:android TextView.setText doesn't work on simple widgetandroid TextView.setText 不适用于简单的小部件
【发布时间】:2011-05-16 13:58:32
【问题描述】:

我有一个非常简单的 AppWidgetProvider 用于测试小部件:

public class Test extends AppWidgetProvider {

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.test_layout);
        views.setTextViewText(R.id.TextView01, "Test message");
    }

}

test_layout 如下所示:

<LinearLayout 
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:id="@+id/TextView01" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </TextView>
</LinearLayout>

问题是小部件出现在模拟器屏幕中但没有任何文本。我确定那是我搞砸了,但我找不到它是什么......

【问题讨论】:

    标签: android android-emulator android-widget


    【解决方案1】:

    您忘记设置要使用的 RemoteViews。您的 AppWidgetProvider 代码应该是这样的:

    public class Test extends AppWidgetProvider { public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){ super.onUpdate(context, appWidgetManager, appWidgetIds); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.test_layout); views.setTextViewText(R.id.TextView01, "Test message"); appWidgetManager.updateAppWidget(appWidgetIds, views); } }

    【讨论】:

    • 谢谢。我也犯了同样的错误。此外,在您的 onUpdate 方法中,您需要放回 super.onUpdate(context, appWidgetManager, appWidgetIds);在第一行。
    猜你喜欢
    • 2012-11-12
    • 2011-10-13
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 2022-10-04
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多