几个按钮的Android小部件onclick监听器

标签 android android-intent android-widget

我正在尝试为我的应用程序创建一个小部件。从我阅读的一个 android 开发者网站来看,你的 onclick 监听器都需要有一个 Intent。但是,如果我只想让我的按钮更新小部件本身中的数据并且我不想开始新的 Activity 呢?

这是一些 Android 演示代码:

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);

我想要一个按钮,当我单击该按钮时会发起一个 http 网络调用,然后在小部件中显示结果。如果我必须使用 Intent ,我该怎么做?我还需要能够区分点击了哪些按钮。

为什么小部件使用 Intent 而不是普通的 onclick 监听器,它调用类似 Activity 的函数?

编辑

我的小部件提供者:

public class MyWidgetProvider extends AppWidgetProvider {

private static final String MyOnClick1 = "myOnClickTag1";
private static final String MyOnClick2 = "myOnClickTag2";
private static final String MyOnClick3 = "myOnClickTag3";

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

    // Get all ids
    ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

    for (int widgetId : allWidgetIds) {

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

        remoteViews.setOnClickPendingIntent(R.id.widget_button_stayarm, getPendingSelfIntent(context, MyOnClick1));
        remoteViews.setOnClickPendingIntent(R.id.widget_button_awayarm, getPendingSelfIntent(context, MyOnClick2));
        remoteViews.setOnClickPendingIntent(R.id.widget_button_dissarm, getPendingSelfIntent(context, MyOnClick3));

        remoteViews.setTextViewText(R.id.widget_textview_gpscoords, "gps cords");

        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
}

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

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

    if (MyOnClick1.equals(intent.getAction())) {
        // your onClick action is here
        Toast.makeText(context, "Button1", Toast.LENGTH_SHORT).show();
        Log.w("Widget", "Clicked button1");
    } else if (MyOnClick2.equals(intent.getAction())) {
        Toast.makeText(context, "Button2", Toast.LENGTH_SHORT).show();
        Log.w("Widget", "Clicked button2");
    } else if (MyOnClick3.equals(intent.getAction())) {
        Toast.makeText(context, "Button3", Toast.LENGTH_SHORT).show();
        Log.w("Widget", "Clicked button3");
    }
};
}

我的 Android list :

<receiver
    android:name="widget.MyWidgetProvider"
    android:icon="@drawable/fsk"
    android:label="FSK Widget" >
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>

    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/example_appwidget_info" />
</receiver>

最佳答案

可以为小部件中的 View 创建 onClick 事件。您可以根据需要创建任意数量的 onClick 事件。

在您的 Widget 类之上,创建一个静态变量,这将是您的 onClick 名称标签:

private static final String MyOnClick = "myOnClickTag";

定义一个辅助方法来自动创建每个PendingIntent:

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

将此 onClick 标记设置为您的 View ,如下所示:

    remoteViews.setOnClickPendingIntent(R.id.button, 
                      getPendingSelfIntent(context, MyOnClick));

在您的 Widget 类中创建一个 onReceive 方法并在其中设置这个 onClick 事件:

public void onReceive(Context context, Intent intent) {

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

每当您设置标签的 View 被按下时,onReceive 都会捕获它并执行与我们日常的标准 onClick 事件相同的操作。

编辑:根据您的回答,您可以用以下几行替换您的 onUpdate 内容并重试:

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_det);
    thisWidget = new ComponentName(context, MyWidgetProvider.class);    
    remoteViews.setOnClickPendingIntent(R.id.widget_button_stayarm, getPendingSelfIntent(context, MyOnClick1));
    remoteViews.setOnClickPendingIntent(R.id.widget_button_awayarm, getPendingSelfIntent(context, MyOnClick2));
    remoteViews.setOnClickPendingIntent(R.id.widget_button_dissarm, getPendingSelfIntent(context, MyOnClick3));
    remoteViews.setTextViewText(R.id.widget_textview_gpscoords, "gps cords");
    appWidgetManager.updateAppWidget(thisWidget, remoteViews);

关于几个按钮的Android小部件onclick监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23220757/

相关文章:

android - 显示来自外部卡的图像给出 java.lang.nullPointerException

java - 自定义硬件按钮事件/Intent

Android Intent 在 Google+ 社区屏幕上启动 Google+ 应用程序

Android - VideoView - onTouch 的行为不符合预期

android - 从暂停恢复时全屏 TabActivity 出现问题

android - 从 Android 小部件查询内容

android - 在 SQLite android 中存储图像

android - 无法使用过滤器从 firebase 获取数据

android - 关于如何在谷歌游戏服务中实现回合制游戏的几个问题

android - 区分 first 中 onCreateView 的调用 first vs followings