如何监听 Xamarin Android 小部件中的项目点击?

发布于 2025-01-10 01:19:23 字数 1936 浏览 0 评论 0原文

问题

我已经实现了此代码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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦巷 2025-01-17 01:19:23

作为测试,我实现了如下功能:点击ListView时,进入APP主页面。

可以参考以下代码:

我们需要调用SetOnClickFillInIntent来区分item的点击行为。

ListProvider.cs

    public RemoteViews GetViewAt(int position)
    {
        RemoteViews remoteView = new RemoteViews(
         context.PackageName, Resource.Layout.list_row);
        ListItem listItem = listItemList[position];
        remoteView.SetTextViewText(Resource.Id.heading, listItem.heading);
        remoteView.SetTextViewText(Resource.Id.content, listItem.content);

       // add code here 
        Bundle extras = new Bundle();
        extras.PutInt("position", position);
        Intent fillInIntent = new Intent();
        fillInIntent.PutExtras(extras);
        // Make it possible to distinguish the individual on-click
        // action of a given item
        remoteView.SetOnClickFillInIntent(Resource.Id.item_frame, fillInIntent);

        return remoteView;

    }

注意:这里item_framelist_row.xml的根元素的id

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:id="@+id/item_frame"
    android:background="@android:color/darker_gray" >

中WidgetProvider.cs

这里,我们在 OnUpdate 方法中设置挂起的意图

    public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        base.OnUpdate(context, appWidgetManager, appWidgetIds);

        int N = appWidgetIds.Length;

        for (int i = 0; i < N; i++)
        {
            RemoteViews remoteViews = updateWidgetListView(context, appWidgetIds[i]);

            // we add code here
            Intent activityIntent = new Intent(context, typeof(MainActivity));
           // activityIntent.SetPackage(context.PackageName);
            // Set the action for the intent.
            // When the user touches a particular view.
            //activityIntent.PutExtra(AppWidgetManager.ExtraAppwidgetId, appWidgetIds[i]);
            //activityIntent.SetData(Android.Net.Uri.Parse(activityIntent.ToUri(Android.Content.IntentUriType.AndroidAppScheme)));
            PendingIntent pendingIntent = PendingIntent.GetActivity(context, appWidgetIds[i], activityIntent, PendingIntentFlags.UpdateCurrent);

            remoteViews.SetPendingIntentTemplate(Resource.Id.listViewWidget, pendingIntent);



            appWidgetManager.UpdateAppWidget(appWidgetIds[i], remoteViews);
        }

    }

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

    public RemoteViews GetViewAt(int position)
    {
        RemoteViews remoteView = new RemoteViews(
         context.PackageName, Resource.Layout.list_row);
        ListItem listItem = listItemList[position];
        remoteView.SetTextViewText(Resource.Id.heading, listItem.heading);
        remoteView.SetTextViewText(Resource.Id.content, listItem.content);

       // add code here 
        Bundle extras = new Bundle();
        extras.PutInt("position", position);
        Intent fillInIntent = new Intent();
        fillInIntent.PutExtras(extras);
        // Make it possible to distinguish the individual on-click
        // action of a given item
        remoteView.SetOnClickFillInIntent(Resource.Id.item_frame, fillInIntent);

        return remoteView;

    }

Note: Here item_frame is the id of root element of list_row.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:id="@+id/item_frame"
    android:background="@android:color/darker_gray" >

In WidgetProvider.cs

Here,we set the pending intent in OnUpdate method

    public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        base.OnUpdate(context, appWidgetManager, appWidgetIds);

        int N = appWidgetIds.Length;

        for (int i = 0; i < N; i++)
        {
            RemoteViews remoteViews = updateWidgetListView(context, appWidgetIds[i]);

            // we add code here
            Intent activityIntent = new Intent(context, typeof(MainActivity));
           // activityIntent.SetPackage(context.PackageName);
            // Set the action for the intent.
            // When the user touches a particular view.
            //activityIntent.PutExtra(AppWidgetManager.ExtraAppwidgetId, appWidgetIds[i]);
            //activityIntent.SetData(Android.Net.Uri.Parse(activityIntent.ToUri(Android.Content.IntentUriType.AndroidAppScheme)));
            PendingIntent pendingIntent = PendingIntent.GetActivity(context, appWidgetIds[i], activityIntent, PendingIntentFlags.UpdateCurrent);

            remoteViews.SetPendingIntentTemplate(Resource.Id.listViewWidget, pendingIntent);



            appWidgetManager.UpdateAppWidget(appWidgetIds[i], remoteViews);
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文