【问题标题】:Set onClick events inside StackView widget items在 StackView 小部件项目中设置 onClick 事件
【发布时间】:2012-07-23 12:58:45
【问题描述】:

我有一个带有集合的小部件(这很重要!)。它看起来像这样:

我的代码基于 Android 官方文档:Android App Widgets

所以对于小部件,我在 StackView 集合中使用带有自定义对象的集合。此集合由 StackRemoteViewsFactory 处理(实现 RemoteViewsService.RemoteViewsFactory)。 如您所见,每个项目都有三个 ImageButtons 和一个 TextView。

我知道如何为整个 RemoteView 项目添加 onClick 行为。 这在官方文档中有描述。

但我的每个视图(按钮和文本视图)都需要四个 onClick 行为。

我的问题: 这是否可以为带有 RemoteView 集合的小部件中的每个 StackView 项目提供不同的 onClicks 视图?

现在我看不到任何可能性:(


更新:

我希望在 onClicks 上有类似的东西,就像这个问题一样:Processing more than one button click at Android Widget。但是由于小部件实现的差异,该问题的解决方案不适用于具有 RemoteViews 集合的小部件。

【问题讨论】:

  • 你有答案吗,因为我也面临同样的问题。你能看看我的问题stackoverflow.com/questions/15965625/…
  • 你解决过这个问题吗?如果您能发布您的解决方案,将会很有帮助。

标签: android android-widget widget onclick stackview


【解决方案1】:

如果您看过 StackView 小部件示例代码,您就会知道有提供程序类和服务类。在服务类中,尝试将 setOnClickFillInIntent 添加到 stackview 布局的每个 id,其中 Intent 包括“命令字”。并在提供程序类中设置 setPendingIntentTemplate 作为示例代码。这是重要的部分,在提供者类中,有 OnReceive()。 setPendingIntentTemplate 将发送 Intent,包括您在 Provider 类中设置的特定操作和从服务类中设置到每个layour id 的“comman word”两件事。那么现在您知道用户从 Stack View 小部件中单击了哪些按钮。 如果您需要更多提示,请告诉我,我将添加示例代码。

【讨论】:

  • 对不起,你的英语听不懂。请提供示例代码。也许它会更简单。谢谢。
  • @user1482049 你能看看我的问题,告诉我我在代码中到底犯了什么错误stackoverflow.com/questions/15965625/…
【解决方案2】:

所以基本上你必须做两件事才能将 onClick 行为添加到 Android App Widget 的 StackView/ListView 中的按钮。

  1. 在实现RemoteViewService.RemoteServiceFactory的StackRemoteViewsFactory的getViewAt函数中, 为要添加功能的 id 添加 setOnClickFillingIntent() 。例如。

     RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget__fan);
     Bundle extras = new Bundle();
     extras.putInt(TestWidget.EXTRA_ITEM, position);
     Intent fillInIntent = new Intent();
     fillInIntent.putExtras(extras);
     rv.setOnClickFillInIntent(R.id.fan_status, fillInIntent); // id of button you want to add a onClick function to.
    
  2. 在扩展 AppWidgetProvider 的 WidgetProvider 类的 onUpdate 函数中, 添加一个 setPendingIntentTemplate()。例如。

     Intent toastIntent = new Intent(context, TestWidget.class);
     toastIntent.setAction(TestWidget.act1);
     toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
     intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
     PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent,PendingIntent.FLAG_UPDATE_CURRENT);
     rv.setPendingIntentTemplate(R.id.lview, toastPendingIntent);
    

现在,已为该特定按钮设置了操作(在我的情况下为 R.id.fan_status)。您可以在 onRecieve() 函数中添加其功能。例如。

public void onReceive(Context context, Intent intent) {    
    if (intent.getAction().equals(act1)) {
       // Add your functionalities
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多