Android start activity when pressing widget ListView item(按下小部件ListView项目时Android开始活动)
问题描述
我正在开发一个 ListView 小部件,我希望用户能够在单击 ListView 时启动一个活动.我还没有找到任何关于这方面的教程,所以我想知道是否有人可以指出我正确的方向或者分享一些代码.无论单击哪个 ListItem,我都想启动相同的活动,所以这不是问题.
I'm working on a ListView widget where I want the user to be able to launch a activity when the ListView is clicked. I haven't been able to find any sort of tutorial on this so I'm wondering if anyone could point me in the right direction or perhaps share some code. I want to launch the same activity regardless of which ListItem is clicked so that's not a problem.
感谢所有帮助!
推荐答案
看看这里并滚动到子标题向单个项目添加行为.
您需要确保从您的 AppWidgetProvider
和 setOnClickFillInIntent()
都调用 setPendingIntentTemplate()
你的 RemoteViewsService.RemoteViewsFactory
实现.
You need to make sure you call both setPendingIntentTemplate()
from your AppWidgetProvider
and setOnClickFillInIntent()
from your RemoteViewsService.RemoteViewsFactory
implementation.
例如:
public class Widget extends AppWidgetProvider {
// ...
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for(int i = 0; i < appWidgetIds.length; i++){
RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent startActivityIntent = new Intent(context, myActivity.class);
PendingIntent startActivityPendingIntent = PendingIntent.getActivity(context, 0, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
widget.setPendingIntentTemplate(R.id.list_view, startActivityPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
// ...
}
}
public class WidgetAdapter implements RemoteViewsService.RemoteViewsFactory {
// ...
@Override
public RemoteViews getViewAt(int position) {
RemoteViews widgetRow = new RemoteViews(context.getPackageName(), R.layout.widget_row);
Intent fillInIntent = new Intent();
fillInIntent.putExtra(Widget.EXTRA_LIST_VIEW_ROW_NUMBER, position);
widgetRow.setOnClickFillInIntent(R.id.list_view_row, fillInIntent);
// ...
return widgetRow;
}
}
SDK 示例中的 StackWidget 示例中有一个更具说服力的示例,虽然我发现它有点难以找到(请参阅此处了解相关说明).它创建了一个显示 Toast 消息的意图,但它使用相同的代码.
There is a more conclusive example in the StackWidget sample which is in the SDK samples, although I found it somewhat difficult to find (see here for directions). It creates an intent to show a Toast message, but it uses the same code.
这篇关于按下小部件ListView项目时Android开始活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:按下小部件ListView项目时Android开始活动


基础教程推荐
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01