ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-08-31 20:24:23  阅读:200  来源: 互联网

标签:homescreen android listview android-widget


我正在使用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"
        style="?android:attr/buttonStyleSmall"
        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);
        }
    }
}

标签:homescreen,android,listview,android-widget
来源: https://codeday.me/bug/20190831/1778186.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有