gpt4 book ai didi

android - 如何在小部件的 TextView 上设置文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:51 25 4
gpt4 key购买 nike

我正在尝试在主屏幕小部件上设置文本。我正在使用 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");
}
}

最佳答案

试试下面的代码。

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

你必须制作一个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>

它将打印小部件中的文本

关于android - 如何在小部件的 TextView 上设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12688566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com