【问题标题】:Can't update Button's text in a Widget无法在小部件中更新按钮的文本
【发布时间】:2013-08-03 16:40:11
【问题描述】:

如何访问主屏幕Widget 的项目,以便对它们进行更改,例如设置Button 的文本?在我的AppWidgetProvideronReceive() 方法中,我试图设置Button 的文本,它位于我的主屏幕小部件中:

@Override
    public void onReceive(Context context, Intent intent)
    {
        if (intent.getAction().equals(MainActivity.ACTION_CHANGE_BUTTONSTATE))
        {
            String state = intent.getExtras().getString("state");
            Log.d("HomescreenWidget", "received broadcast for button state");

            RemoteViews views = new RemoteViews(context.getPackageName(),
                    R.id.widgetButton);
            views.setTextViewText(R.id.widgetButton, state);
            ComponentName cn = new ComponentName(context,
                    HomescreenWidget.class);
            AppWidgetManager.getInstance(context).updateAppWidget(cn, views);
        }
        super.onReceive(context, intent);
    }

我的小部件的layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/widget_margin" >

    <Button
        android:id="@+id/playButton_Widget"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:minWidth="80dp"
        android:text="@string/playButton" />
</RelativeLayout>

我收到了Broadcast,但随后我的Widget 显示以下文本:“问题加载小部件”。

编辑 我在目录日志中发现了以下消息:

W/AppWidgetHostView(135): updateAppWidget couldn't find any view, using error view
W/AppWidgetHostView(135): android.widget.RemoteViews$ActionException: can't find view: 0x7f0a0004

0x7f0a0004 是我的WidgetButton。找不到的原因是什么?

【问题讨论】:

  • 抱歉,只是想再次确认您想从主屏幕小部件更改应用中的某些视图?
  • 我有一个Button 属于主屏幕Widgetlayout。按下此按钮时,我想更改其文本标签。为此,我需要参考它
  • 主屏幕小部件中的文本标签?
  • a Button,一个按钮,位于主屏幕小部件中。这个按钮有一个像这样的文本标签:lh4.googleusercontent.com/_VZJksQ6kepc/TZCBDL7bqnI/AAAAAAAAAC8/…

标签: android widget homescreen remoteview appwidgetprovider


【解决方案1】:

要通过 appwidget 提供程序访问您的视图,您可以使用此远程视图

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setTextViewText(R.id.widgetname, newName);

或通过服务:

public class UpdateWidgetService extends Service {

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override
  public void onStart(Intent intent, int startId) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
        .getApplicationContext());

    int[] allWidgetIds = intent
        .getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

    for (int widgetId : allWidgetIds) {

      RemoteViews remoteViews = new RemoteViews(this
          .getApplicationContext().getPackageName(),
          R.layout.widget_layout);

      remoteViews.setTextViewText(R.id.update,
          "Random: " + String.valueOf(number));

      appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
    stopSelf();

    super.onStart(intent, startId);
  }

} 

别忘了将服务添加到清单中

<service android:name=".UpdateWidgetService"></service> 

然后在 onUpdate 中(在您的 App Widget Provider 类中)

@Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
      int[] appWidgetIds) {

    ComponentName thisWidget = new ComponentName(context,
        MyWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

    Intent intent = new Intent(context.getApplicationContext(),
        UpdateWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

    context.startService(intent);
  }

我希望这可以帮助:)

【讨论】:

  • 我已经按照你所说的通过远程视图进行了尝试(我编辑了问题,所以你可以看到我做了什么)。什么都没有发生。
【解决方案2】:

我刚刚找到了解决方案。我一直在为按钮传递错误的 ID。 widgetButton 不存在于 Widget 的布局中。这是我主要的Activity中的一个按钮

【讨论】:

    【解决方案3】:

    导入android.widget.RemoteViews;

    //抓取布局,然后设置名为R.id.Counter的Button的文本:

    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_layout);
    remoteViews.setTextViewText(R.id.Counter, "Set button text here");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 2011-04-20
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 2014-09-20
      • 2015-01-02
      相关资源
      最近更新 更多