Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在小部件中处理多个按钮-Android_Java_Android_Widget_Android Widget - Fatal编程技术网

Java 在小部件中处理多个按钮-Android

Java 在小部件中处理多个按钮-Android,java,android,widget,android-widget,Java,Android,Widget,Android Widget,我有一个小部件,显示一个简单的文本和3个按钮: 刷新(拾取另一个随机字符串并显示它) 复制(将文本视图的内容复制到剪贴板) 共享(将文本视图的内容共享到社交媒体等) 我已经有了刷新按钮的设置,工作正常,但我似乎无法找到一种方法来处理其他两个按钮 PS.我不需要实际的代码,我已经知道如何进行复制和共享,我只需要知道如何处理点击事件 以下是我目前的代码: Button copy_content; Button share_content; void updateAppWidget(Contex

我有一个小部件,显示一个简单的文本和3个按钮:

  • 刷新(拾取另一个随机字符串并显示它)
  • 复制(将文本视图的内容复制到剪贴板)
  • 共享(将文本视图的内容共享到社交媒体等)
我已经有了刷新按钮的设置,工作正常,但我似乎无法找到一种方法来处理其他两个按钮

PS.我不需要实际的代码,我已经知道如何进行复制和共享,我只需要知道如何处理点击事件

以下是我目前的代码:

Button copy_content;
Button share_content;

void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
    /** Code below will be executed once the timer is over*/
    String widgetText = RandQuotes[rand.nextInt(RandQuotes.length)];

    // Construct the RemoteViews object
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.quotes_widget);
    views.setTextViewText(R.id.sayings, widgetText);



    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views);
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // There may be multiple widgets active, so update all of them
    for (int appWidgetId : appWidgetIds) {
        updateAppWidget(context, appWidgetManager, appWidgetId);
    }
    final int count = appWidgetIds.length;

    for (int i = 0; i < count; i++) {
        int widgetId = appWidgetIds[i];
        String on_sayings = RandQuotes[rand.nextInt(RandQuotes.length)];

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.quotes_widget);
        remoteViews.setTextViewText(R.id.sayings, on_sayings);

        Intent intent = new Intent(context, HavamalWidget.class);
        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.switch_trigger, pendingIntent);
        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }

}
按钮复制内容;
按钮共享内容;
void updateAppWidget(上下文上下文,AppWidgetManager AppWidgetManager,int appWidgetId){
/**定时器结束后,将执行以下代码*/
字符串widgetText=RandQuotes[rand.nextInt(RandQuotes.length)];
//构造RemoteView对象
RemoteView视图=新的RemoteView(context.getPackageName(),R.layout.quotes\u小部件);
views.setTextViewText(R.id.sayings,widgetText);
//指示小部件管理器更新小部件
UpdateAppWidgetManager.UpdateAppWidgetId(appWidgetId,视图);
}
@凌驾
公共void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,int[]AppWidgetId){
//可能有多个窗口小部件处于活动状态,因此请更新所有窗口小部件
for(int-appWidgetId:appWidgetId){
updateAppWidget(上下文、appWidgetManager、appWidgetId);
}
最终整数计数=appWidgetIds.length;
for(int i=0;i
只需在按钮上用动作做出挂起的意图即可。所以在onReceive()中,只需检查意图的动作,并对其进行一些逻辑处理。如果要执行一些长任务,最好在IntentService中创建所有逻辑

在RemoteView中的按钮上设置挂起的意图,如下所示:

public static final String ACTION_BUTTON_SHARE = "ACTION_BUTTON_SHARE";
public static final String ACTION_BUTTON_REFRESH = "ACTION_BUTTON_REFRESH";

Intent refreshIntent = new Intent(context, ExampleAppWidget.class)
            .setAction(ACTION_BUTTON_REFRESH);
PendingIntent refreshPI = PendingIntent.getBroadcast(context, 0, refreshIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.refresh_button, refreshPI);

Intent shareIntent = new Intent(context, ExampleAppWidget.class)
            .setAction(ACTION_BUTTON_SHARE);
PendingIntent sharePI = PendingIntent.getBroadcast(context, 0, shareIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.share_button, sharePI);
在示例AppWidget.class(从AppWidgetProvider扩展而来)中,重写OnReceive()方法


只需在按钮上用动作做出悬而未决的意图。所以在onReceive()中,只需检查意图的动作,并对其进行一些逻辑处理。如果要执行一些长任务,最好在IntentService中创建所有逻辑

在RemoteView中的按钮上设置挂起的意图,如下所示:

public static final String ACTION_BUTTON_SHARE = "ACTION_BUTTON_SHARE";
public static final String ACTION_BUTTON_REFRESH = "ACTION_BUTTON_REFRESH";

Intent refreshIntent = new Intent(context, ExampleAppWidget.class)
            .setAction(ACTION_BUTTON_REFRESH);
PendingIntent refreshPI = PendingIntent.getBroadcast(context, 0, refreshIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.refresh_button, refreshPI);

Intent shareIntent = new Intent(context, ExampleAppWidget.class)
            .setAction(ACTION_BUTTON_SHARE);
PendingIntent sharePI = PendingIntent.getBroadcast(context, 0, shareIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.share_button, sharePI);
在示例AppWidget.class(从AppWidgetProvider扩展而来)中,重写OnReceive()方法


我在遵循您的说明时迷路了,您能发布完整的答案吗?请再次查看答案我在
CommonRemoteViewBuilder
和操作按钮
action\u-BUTTON\u-COPY
action\u-BUTTON\u-REFRESH
上遇到了一些问题。我在遵循您的说明时迷路了,你能发布完整的答案吗?只需再次查看答案我对
CommonRemoteViewBuilder
和操作按钮
action\u-BUTTON\u-COPY
action\u-BUTTON\u-REFRESH
有一些问题,上面写着“无法解析符号”