3

我正在使用 RemoteViewsService.RemoteViewsFactory (链接

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

这是我的小部件布局 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]);
      // http://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);

}

如何捕获按钮单击???

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

非常感谢你的帮助!

4

2 回答 2

3

如您提供的页面所述:

此 AppWidgetProvider 仅定义 onUpdate() 方法,用于定义启动 Activity 的 PendingIntent 并使用 setOnClickPendingIntent(int, PendingIntent) 将其附加到 App Widget 的按钮。请注意,它包含一个循环,该循环遍历 appWidgetIds 中的每个条目,这是一个 ID 数组

还:

如使用 AppWidgetProvider 类中所述,您通常使用 setOnClickPendingIntent() 来设置对象的点击行为——例如使按钮启动 Activity。但是这种方法不允许用于单个集合项中的子视图(澄清一下,您可以使用 setOnClickPendingIntent() 在启动应用程序的 Gmail 应用程序小部件中设置一个全局按钮,但不能在单个列表项上)。相反,要将点击行为添加到集合中的单个项目,您可以使用 setOnClickFillInIntent()。这需要为您的集合视图设置一个待处理的意图模板,然后通过您的 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);
        }
    }
}
于 2013-07-02T00:38:13.460 回答
2

这就是我最终做的......

for (int i=0; i<appWidgetIds.length; i++) {

      Intent svcIntent=new Intent(context, WidgetService.class);
      svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
      // http://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 ... ?   

      Intent btn_clickIntent = new Intent(context, Config.class);
      btn_clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
      PendingIntent btn_pendingIntent = PendingIntent.getActivity(context, 0, btn_clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      remoteviews.setOnClickPendingIntent(R.id.button1, btn_pendingIntent);     

      Intent btn2_clickIntent = new Intent(context, Config2.class);
      btn2_clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
      PendingIntent btn2_pendingIntent = PendingIntent.getActivity(context, 0, btn2_clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      remoteviews.setOnClickPendingIntent(R.id.button2, btn2_pendingIntent);        

      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);  
}
于 2013-07-02T21:41:27.400 回答