【问题标题】:How to set onClickListener on button inside Widget?如何在 Widget 内的按钮上设置 onClickListener?
【发布时间】:2015-07-15 12:19:18
【问题描述】:

我已经尝试按照这个solution去做。但它在某种程度上不起作用。 没有错误,这可能会提供解决方法的线索。

在我的清单中,我有这个接收器:

<receiver android:name=".ImageReceiver">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
        android:resource="@xml/widget_image" />
</receiver>

widget_image.xml:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="72dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="1800000"
    android:initialLayout="@layout/widget_layout">
</appwidget-provider>

widget_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ivImage"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/bRefresh" />

</RelativeLayout>

ImageReceiver 看起来像这样:

public class ImageReceiver extends AppWidgetProvider {

    public static final String REFRESH_BUTTON_CLICKED = "REFRESH_BUTTON_CLICKED";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        remoteViews.setOnClickPendingIntent(R.id.bRefresh, getPendingSelfIntent(context, REFRESH_BUTTON_CLICKED));
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);

        if (REFRESH_BUTTON_CLICKED.equals(intent.getAction())) {
            Toast.makeText(context, "Button click", Toast.LENGTH_LONG).show();
        }

    }

    protected PendingIntent getPendingSelfIntent(Context context, String action) {
        Intent intent = new Intent(context, getClass());
        intent.setAction(action);
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }
}

当我单击按钮 bRefresh 时,它应该显示 Toast“按钮单击”,但它没有显示。如何解决这个问题?

【问题讨论】:

  • 尝试返回PendingIntent.getActivity(context, 0, intent, 0);
  • @MD,没有效果。我还能做什么?

标签: android android-widget


【解决方案1】:

在你的更新中,

views.setOnClickPendingIntent(R.id.widget_retry, getPendingSelfIntent(context, MyOnClick));

比拿,

    private static final String MyOnClick = "myOnClickTag";

    protected PendingIntent getPendingSelfIntent(Context context, String action)
    {
        // if we brodcast than definetly we have to define ONRECEIVE
        Intent intent = new Intent(context, getClass());
        intent.setAction(action);
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }

和,

@Override
public void onReceive(Context context, Intent intent)
{
    // TODO Auto-generated method stub
    super.onReceive(context, intent);

            if (MyOnClick.equals(intent.getAction()))
            {
                //your onClick action is here

            }
}

上面的代码为我工作!

【讨论】:

    【解决方案2】:

    每当您创建自己的操作时,您都必须在您的 AndroidManifest.xml 文件中注册它。所以在你的 AndroidManifest.xml 文件的接收器->intent-filter 中添加这一行。

    <receiver android:name=".ImageReceiver">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        <action android:name="REFRESH_BUTTON_CLICKED"/> // Add this Line
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
        android:resource="@xml/widget_image" />
    </receiver>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      • 2011-12-28
      • 2019-05-06
      • 2012-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多