1
我已经在家中scrren小部件,并且我正在生成一些项目(我有这个项目的布局)小部件并在它们上设置了一些监听器。这是非常简单的,就像这样:动态创建项目的监听器
RemoteViews currentItem1 = new RemoteViews(context.getPackageName(), R.layout.complex_list_item);
updateViews.addView(R.id.panelNews, currentItem1);
Intent intent1 = new Intent(context, ForexWidget.class);
intent1.setAction(Constants.ACTION_PRESSED_ITEM);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, intent1, 0);
updateViews.setOnClickPendingIntent(R.id.singleLine, pendingIntent1);
RemoteViews currentItem2 = new RemoteViews(context.getPackageName(), R.layout.complex_list_item);
updateViews.addView(R.id.panelNews, currentItem2);
Intent intent2 = new Intent(context, ForexWidget.class);
intent2.setAction(Constants.ACTION_PRESSED_ITEM);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, intent2, 0);
updateViews.setOnClickPendingIntent(R.id.singleLine, pendingIntent2);
项目创建,加入到小部件,它看起来很棒,可是听者不工作我想要的方式 - 只对第一个按钮的作品第一个监听,别人没有按“T。我想它是因为我注册setOnClickPendingIntent为两个按钮具有相同的ID(R.id.singleLine) - 但最大的问题是,我不能设置按钮的ID时,其内的远程视图...
有没有某种方式做这样的事情? Thx很多
编辑:这是我的布局项目,我试图生成。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/singleLine"
android:layout_width="fill_parent"
android:layout_height="34dp"
android:clickable="true"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTime"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:paddingRight="5dp"
android:textSize="@dimen/fontNewsCompact" />
<TextView
android:id="@+id/txtCurrency"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:paddingRight="5dp"
android:textSize="@dimen/fontNewsExtraCompact" />
</LinearLayout>
好,对我来说,不工作 - 当然,由于IM设置onClickPendingIntent 2倍的你可以看到相同的id。一天的问题是,如何避免这...有关更多信息,我已经更新了我的第一篇文章,并添加了我想要生成的布局(使用id singleLine) – qkx
我知道您使用的是同一个ID,但它一定是不同的。如果您使用该Id在您的ForexWidget类中创建一些逻辑,请使用action.startsWith(Constants.Action_PRESSED_ITEM)而不是平等。 – htafoya
我完全理解你,但它不是主要问题。问题是,我正在执行'updateViews.setOnClickPendingIntent(R.id.singleLine,pendingIntent1)'和这个'updateViews.setOnClickPendingIntent(R.id.singleLine,pendingIntent2)''所以实际上我注册了2个onclick intents for one项目'R.id.singleLine' - 我也有2个实例。但如何正确注册?因为像这样'updateViews.setOnClickPendingIntent(currentItem1.getLayoutId(),pendingIntent1)'和这个'updateViews.setOnClickPendingIntent(currentItem2.getLayoutId(),pendingIntent2)'当然不起作用。 – qkx