android– 在Homescreen小部件的listview上方添加一个按钮 – 如何捕获单击此按钮?

android– 在Homescreen小部件的listview上方添加一个按钮 – 如何捕获单击此按钮?,第1张

概述我正在使用RemoteViewsService.RemoteViewsFactory(link)在主屏幕小部件中显示列表视图.一切正常,我可以捕获listview项目的点击.现在我想在listview上方添加一个按钮,以允许用户跳转到配置活动:这是我的小部件布局xml:<LinearLayoutxmlns:android="http://schemas.android.com/

我正在使用RemoteVIEwsService.RemoteVIEwsFactory(link)

在主屏幕小部件中显示列表视图.一切正常,我可以捕获ListvIEw项目的点击.现在我想在ListvIEw上方添加一个按钮,以允许用户跳转到配置活动:

这是我的小部件布局xml:

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:ID="@+ID/layout"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:layout_margin="8dip"    androID:background="@drawable/Widget_frame"    androID:orIEntation="vertical" > <button        androID:ID="@+ID/button1"                androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="button" /><ListVIEw androID:ID="@+ID/ListvIEw"  androID:layout_wIDth="match_parent"  androID:layout_height="match_parent"  androID:layout_margintop="0dp"  androID:layout_marginleft="0dp"/></linearLayout> 

这是我在WidgetProvIDer.java类中的onUpdate方法:

public voID onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIDs) {  Log.w(LOG, "onUpdate method called");  for (int i=0; i<appWidgetIDs.length; i++) {      Intent svcIntent=new Intent(context, WidgetService.class);      svcIntent.putExtra(AppWidgetManager.EXTRA_APPWidget_ID, appWidgetIDs[i]);      // https://stackoverflow.com/questions/13199904/androID-home-screen-Widget-remotevIEws-setremoteadapter-method-not-working      Random generator = new Random();      int randomNumber = generator.nextInt(1000);      svcIntent.putExtra("random", randomNumber);      svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));      RemoteVIEws remotevIEws=new RemoteVIEws(context.getPackagename(), R.layout.Widget_layout);      //------------------------------------------------------------------------------------      // trying to capture the button click ... ?      RemoteVIEws btn_remotevIEws=new RemoteVIEws(context.getPackagename(), R.ID.button1);            Intent btn_clickIntent = new Intent(context, Config.class);      btn_clickIntent.putExtra(AppWidgetManager.EXTRA_APPWidget_ID, appWidgetIDs[i]);      //PendingIntent btn_pendingIntent = PendingIntent.getbroadcast(context, appWidgetIDs[i], btn_clickIntent, 0);      PendingIntent btn_pendingIntent = PendingIntent.getActivity(context, 0, btn_clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);      btn_remotevIEws.setPendingIntentTemplate(R.ID.button1, btn_pendingIntent);              appWidgetManager.updateAppWidget(appWidgetIDs[i], btn_remotevIEws);      //------------------------------------------------------------------------------------      remotevIEws.setRemoteAdapter(appWidgetIDs[i], R.ID.ListvIEw, svcIntent);      Intent clickIntent=new Intent(context, LoremActivity.class);      clickIntent.putExtra(AppWidgetManager.EXTRA_APPWidget_ID, appWidgetIDs[i]);      PendingIntent clickPI=PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);      remotevIEws.setPendingIntentTemplate(R.ID.ListvIEw, clickPI);             appWidgetManager.updateAppWidget(appWidgetIDs[i], remotevIEws);  }super.onUpdate(context, appWidgetManager, appWidgetIDs);

}

如何捕获按钮单击???

我的列表视图上的所有点击都被捕获但我无法捕获ListvIEw上方按钮的点击???

非常感谢你的帮助!

解决方法:

如您提供的页面所述:

This appwidgetprovider defines only the onUpdate() method for the
purpose of defining a PendingIntent that launches an Activity and
attaching it to the App Widget’s button with
setonClickPendingIntent(int, PendingIntent). Notice that it includes a
loop that iterates through each entry in appWidgetIDs, which is an
array of IDs that IDe

也:

As described in Using the appwidgetprovider Class, you normally use
setonClickPendingIntent() to set an object’s click behavior—such as to
cause a button to launch an Activity. But this approach is not allowed
for child vIEws in an indivIDual collection item (to clarify, you
Could use setonClickPendingIntent() to set up a global button in the
Gmail app Widget that launches the app, for example, but not on the
indivIDual List items). Instead, to add click behavior to indivIDual
items in a collection, you use setonClickFillinIntent(). This entails
setting up up a pending intent template for your collection vIEw, and
then setting a fill-in intent on each item in the collection via your
RemoteVIEwsFactory.

您提供的链接中的示例怎么样?我没有仔细阅读那个页面(你肯定应该),但是你不应该setonClickPendingIntent()或setonClickFillinIntent()?:

public class Exampleappwidgetprovider extends appwidgetprovider {    public voID onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIDs) {        final int N = appWidgetIDs.length;        // Perform this loop procedure for each App Widget that belongs to this provIDer        for (int i=0; i<N; i++) {            int appWidgetID = appWidgetIDs[i];            // Create an Intent to launch ExampleActivity            Intent intent = new Intent(context, ExampleActivity.class);            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);            // Get the layout for the App Widget and attach an on-click Listener            // to the button            RemoteVIEws vIEws = new RemoteVIEws(context.getPackagename(), R.layout.appWidget_provIDer_layout);            vIEws.setonClickPendingIntent(R.ID.button, pendingIntent);            // Tell the AppWidgetManager to perform an update on the current app Widget            appWidgetManager.updateAppWidget(appWidgetID, vIEws);        }    }}
总结

以上是内存溢出为你收集整理的android – 在Homescreen小部件的listview上方添加一个按钮 – 如何捕获单击此按钮?全部内容,希望文章能够帮你解决android – 在Homescreen小部件的listview上方添加一个按钮 – 如何捕获单击此按钮?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1106752.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存