开发者

android TextView.setText doesn't work on simple widget

开发者 https://www.devze.com 2023-01-27 09:19 出处:网络
I have the extremely simple AppWidgetProvider for a test widget: public class Test extends AppWidgetProvider {

I have the extremely simple AppWidgetProvider for a test widget:

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

}

The test_layout looks like this:

<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/Tex开发者_如何学运维tView01" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </TextView>
</LinearLayout>

The problem is that the widget appears in the emulator screen but without any text. I'm sure that's i'm messing something but i can't find what it is...


You forgot to set the RemoteViews to use. Your code for AppWidgetProvider should be this:


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

}

0

精彩评论

暂无评论...
验证码 换一张
取 消