如何在 Android 小部件上实现按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2082998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement a Button on an Android Widget
提问by bigtony
I am just getting started with Android development and I have created a nice little widget that displays some info on my home screen. However, I now want to implement a Button on my widget that updates the info in my widget TextView.
我刚刚开始使用 Android 开发,我创建了一个漂亮的小部件,可以在我的主屏幕上显示一些信息。但是,我现在想在我的小部件上实现一个按钮来更新我的小部件 TextView 中的信息。
Can anyone advise how to go about doing this?
任何人都可以建议如何去做吗?
Thanks
谢谢
采纳答案by bigtony
Solved - I can confirm that an Activity is NOT needed if you want create a Button to update an Android AppWidget.
已解决 - 如果您想创建一个按钮来更新 Android AppWidget,我可以确认不需要 Activity。
I have been able to implement my AppWidgetProvider class such that it registers an android.appwidget.action.APPWIDGET_UPDATE intent-filter with the Broadcast receiver in the AndroidManifest.xml, which then fires the onUpdate event in the AppWidgetProvider class (which in turn then runs the UpdateService).
我已经能够实现我的 AppWidgetProvider 类,以便它向 AndroidManifest.xml 中的广播接收器注册一个 android.appwidget.action.APPWIDGET_UPDATE 意图过滤器,然后在 AppWidgetProvider 类中触发 onUpdate 事件(然后运行更新服务)。
<!-- Broadcast Receiver that will process AppWidget updates -->
<receiver android:name=".MyWidget" android:label="@string/widget_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />
</receiver>
The UpdateService in my AppWidgetProvider class then uses onHandleIntent to run a private buildUpdate method - which registers the onClick event with a call to setOnClickPendingIntent as follows:
我的 AppWidgetProvider 类中的 UpdateService 然后使用 onHandleIntent 运行私有 buildUpdate 方法 - 该方法通过调用 setOnClickPendingIntent 注册 onClick 事件,如下所示:
// set intent and register onclick
Intent i = new Intent(this, MyWidget.class);
PendingIntent pi = PendingIntent.getBroadcast(context,0, i,0);
updateViews.setOnClickPendingIntent(R.id.update_button,pi);
Here is a link to some source code of a working example, which shows how an update button can be used to update a Twitter widget:
这是一个工作示例的一些源代码的链接,它显示了如何使用更新按钮来更新 Twitter 小部件:
回答by Alex Volovoy
Button is supported in appwidget so not sure what the problem is. Look at this example on how assign actions via views.setOnClickPendingIntent(R.id.YOURVIEW ID, yourIntent);
appwidget 支持按钮,所以不确定是什么问题。看看这个例子,了解如何通过 views.setOnClickPendingIntent(R.id.YOURVIEW ID, yourIntent); 分配动作。
A RemoteViews object (and, consequently, an App Widget) can support the following layout classes:
FrameLayout LinearLayout RelativeLayout
And the following widget classes:
AnalogClock Button Chronometer ImageButton ImageView ProgressBar TextView Descendants of these classes are not supported.
RemoteViews 对象(以及相应的 App Widget)可以支持以下布局类:
FrameLayout LinearLayout 相对布局
以及以下小部件类:
AnalogClock Button Chronometer ImageButton ImageView ProgressBar TextView 不支持这些类的后代。