【问题标题】:Weird onclick behavior of images on home screen widget主屏幕小部件上图像的奇怪点击行为
【发布时间】:2010-04-07 22:03:26
【问题描述】:

我编写了一个带有一张图片的主屏幕小部件。单击图像时,将打开浏览器以获取 url 链接。 一般来说,它正在工作。但奇怪的是,当我点击背景,然后点击图片时,浏览器将无法打开。直到我第二次点击图片,浏览器才会打开。重现步骤如下:

  1. 点击主屏幕小部件背景。
  2. 单击主屏幕上的图像。浏览器未打开。
  3. 再次单击图像。浏览器已打开。

如果我没有点击背景,图片对点击的反应非常好,即第一次点击图片时浏览器会打开。

小部件 XML 文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget"
android:layout_width="320dip" android:layout_height="200dip"
    android:background="@drawable/prt_base"
>
    <ImageView android:id="@+id/picture1"
        android:layout_width="134dip"
        android:layout_height="102dip"
        android:layout_marginLeft="62dip"
        android:layout_marginTop="6dip"
        android:scaleType="center"
                android:src="@drawable/picture1"
    />
</RelativeLayout>

在picture1 ImageView上设置OnClick的代码如下:

                        defineIntent = new Intent(Intent.ACTION_VIEW, Uri
                                .parse("http://www.google.com"));
                        pendingIntent = PendingIntent
                                .getActivity(context,
                                        0 /* no requestCode */,
                                        defineIntent, 0 /* no flags */);
                        updateViews.setOnClickPendingIntent(
                                picId, pendingIntent);

有人知道怎么回事吗?

谢谢。

【问题讨论】:

  • 您可能想发布相关代码。在我看来,这些信息不足以进行诊断。
  • 我刚刚在我的问题中添加了代码。谢谢。
  • 去掉图片的边距会发生什么?

标签: android


【解决方案1】:
  RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.mywidget);

    Intent defineIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
        PendingIntent pendingIntent = PendingIntent.getActivity(context,
                0 , defineIntent, 0 );
   updateViews.setOnClickPendingIntent(picId, pendingIntent);

   appWidgetManager.updateAppWidget(appWidgetId, updateViews );

它会起作用的!

你能发布你的 appwidget-provider 吗???

【讨论】:

  • 我的代码和你的一模一样。它大部分时间都有效,除了如果我单击小部件的背景区域而不是图像,那么我必须单击图像两次才能打开浏览器。
  • 我通过替换 updateViews.setOnClickPendingIntent(R.id.widget, pendingIntent); 中的一个语句行来更改 android-developers.blogspot.com/2009/04/… 中的代码更新Views.setOnClickPendingIntent(R.id.definition,pendingIntent);修改后的代码具有与我的代码完全相同的奇怪行为。也许这是 Android 中的错误?
  • 是的,我遇到了同样的问题,我正在用纸杯蛋糕进行测试:o
猜你喜欢
  • 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
相关资源
最近更新 更多