6

ホーム画面ウィジェットにテキストを設定しようとしています。これをアーカイブするためにremoteViewを使用していますが、アプリケーションを実行してもテキストが表示されません。テキストは TextView に設定する必要があります (「更新」というラベルが付いています)。

public class MyWidgetProvider extends AppWidgetProvider 
{       
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
  {
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    views.setTextViewText(R.id.update, "Hellow");
  }
}
4

2 に答える 2

20

以下のコードを試してください。

static void updateAppWidget(Context context,
                 AppWidgetManager appWidgetManager,  int appWidgetId) {

            DateFormat format = SimpleDateFormat.getTimeInstance(
                    SimpleDateFormat.MEDIUM, Locale.getDefault());
            CharSequence text = format.format(new Date());


            Intent intent = new Intent(context, UpdateService.class);
            PendingIntent pendingIntent = PendingIntent.getService(context, 0,
                    intent, 0);


            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.widget);
            remoteViews.setOnClickPendingIntent(R.id.LinearLayout01, pendingIntent);

            remoteViews.setTextViewText(R.id.widget_textview, text); <---- here you can set text

            // Tell the widget manager
            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

1つのxmlファイルを作成する必要があります

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_height="120dp"
    android:layout_width="320dp"
    android:orientation="horizontal"
    >

<TextView  
    android:id="@+id/widget_textview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical|center_horizontal"
    android:textColor="@android:color/white"
    android:text="00:00:00 PM"
    android:textSize="16sp"
    android:background="#FFAADD"
    />


</LinearLayout>

ウィジェットのテキストを印刷します

于 2012-10-02T10:36:27.423 に答える
0

代わりに、このコードを試して textView のテキストを設定してください

public class MyWidgetProvider extends Activity
{
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.widget_layout);
TextView t1=(TextView)findViewById(R.id.update);
t1.setText("Hello");

}
于 2012-10-02T10:31:07.987 に答える