如何监听 Xamarin Android 小部件中的项目点击?
问题
我已经实现了此代码AppWidgetListView我尝试附加点击次数。
进展顺利:因此,单击小部件中但位于 ListView 之外的按钮是有效的。您可以在实现 AppWidgetProvider 的类 (WordWidget) 的 OnReceive 方法上看到按钮的单击结果。
什么不起作用:当您从 ListView 单击某个项目(或该项目中的元素)时,它不会显示任何内容。
尝试:
示例 1
Intent configIntent = new Intent(context, typeof(WordWidget));
configIntent.PutExtra("appWidgetId", ids[i]);
PendingIntent configPendingIntent = PendingIntent.GetActivity(context, 0, configIntent, 0);
remoteViews.SetOnClickPendingIntent(Resource.Id.button, configPendingIntent);
示例 2
Intent active = new Intent(context, typeof(WordWidget));
active.SetAction("aaa");
PendingIntent actionPendingIntent = PendingIntent.GetBroadcast(context, 0, active, 0);
remoteViews.SetOnClickPendingIntent(Resource.Id.button, actionPendingIntent);
示例 3
var intent = new Intent(context, typeof(WordWidget));
intent.SetAction(AppWidgetManager.ActionAppwidgetUpdate);
intent.PutExtra(AppWidgetManager.ExtraAppwidgetIds, ids);
var piBackground = PendingIntent.GetBroadcast(context, 0, intent, PendingIntentFlags.UpdateCurrent);
remoteViews.SetOnClickPendingIntent(Resource.Id.button, piBackground);
示例 4
var intent = new Intent(context, typeof(WordWidget));
intent.SetAction("test");
remoteViews.SetOnClickPendingIntent(Resource.Id.button, PendingIntent.GetBroadcast(context, 0, intent, 0));
示例 5
Intent intent = new Intent(Intent.ActionView, Android.Net.Uri.Parse("https://google.com"));
remoteViews.SetOnClickPendingIntent(Resource.Id.button, intent);
我尝试用 typeof(MainActivity) 替换 typeof(WordWidget),但它不起作用。我尝试将这些代码放在 ListProvider 的 public RemoteViews GetViewAt(intposition) 上,其中构建了项目布局,但它不起作用。
The Problem
I've implemented this code AppWidgetListView and I've tried to attach clicks.
What's going well: so, clicks from buttons from widget but outside the ListView, works. You can see the click result from that buttons on OnReceive method from a class (WordWidget) that implemented AppWidgetProvider.
what does not work: when you click an item (or an element from that item) from ListView, it doesn't show a thing.
Attempts:
Example 1
Intent configIntent = new Intent(context, typeof(WordWidget));
configIntent.PutExtra("appWidgetId", ids[i]);
PendingIntent configPendingIntent = PendingIntent.GetActivity(context, 0, configIntent, 0);
remoteViews.SetOnClickPendingIntent(Resource.Id.button, configPendingIntent);
Example 2
Intent active = new Intent(context, typeof(WordWidget));
active.SetAction("aaa");
PendingIntent actionPendingIntent = PendingIntent.GetBroadcast(context, 0, active, 0);
remoteViews.SetOnClickPendingIntent(Resource.Id.button, actionPendingIntent);
Example 3
var intent = new Intent(context, typeof(WordWidget));
intent.SetAction(AppWidgetManager.ActionAppwidgetUpdate);
intent.PutExtra(AppWidgetManager.ExtraAppwidgetIds, ids);
var piBackground = PendingIntent.GetBroadcast(context, 0, intent, PendingIntentFlags.UpdateCurrent);
remoteViews.SetOnClickPendingIntent(Resource.Id.button, piBackground);
Example 4
var intent = new Intent(context, typeof(WordWidget));
intent.SetAction("test");
remoteViews.SetOnClickPendingIntent(Resource.Id.button, PendingIntent.GetBroadcast(context, 0, intent, 0));
Example 5
Intent intent = new Intent(Intent.ActionView, Android.Net.Uri.Parse("https://google.com"));
remoteViews.SetOnClickPendingIntent(Resource.Id.button, intent);
I've tried to replace typeof(WordWidget) with typeof(MainActivity) and it does not work. and I've tried to put that codes on public RemoteViews GetViewAt(int position) from ListProvider where the item layout is built and it does not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为测试,我实现了如下功能:点击ListView时,进入APP主页面。
可以参考以下代码:
我们需要调用
SetOnClickFillInIntent
来区分item的点击行为。在
ListProvider.cs
中注意:这里
item_frame
是list_row.xml
的根元素的id在
中WidgetProvider.cs
这里,我们在
OnUpdate
方法中设置挂起的意图As a test, I implemented the following function: When clicking on the ListView, enter the main page of the APP.
You can refer to the following code:
We need to call
SetOnClickFillInIntent
to differentiate the item on-click behavior.In
ListProvider.cs
Note: Here
item_frame
is the id of root element oflist_row.xml
In
WidgetProvider.cs
Here,we set the pending intent in
OnUpdate
method