【问题标题】:Set ImageView source on click with RemoteViews使用 RemoteViews 单击设置 ImageView 源
【发布时间】:2015-03-15 20:05:30
【问题描述】:

有没有办法通过远程视图在点击时设置图像视图的来源。我知道你能做到 remoteViews.setImageViewResource(R.id.image, R.drawable.source);

但这不允许我在点击时执行此操作。

任何帮助将不胜感激。 谢谢。

【问题讨论】:

    标签: android onclick android-imageview remoteview android-remoteview


    【解决方案1】:

    但这不允许我在点击时这样做

    是的,确实如此。在RemoteViews 中的ImageView 条目上调用setOnClickPendingIntent(),使用指向您的AppWidgetProvider 或另一个BroadcastReceiverPendingIntent。在那里,致电setImageResource() 以更新您的ImageView 并通过AppWidgetManager 将更新推送到主屏幕。

    例如,此示例 AppWidgetProvider 会在用户点击任一骰子时使用新的随机骰子滚动更新一对 ImageView 小部件:

    /***
      Copyright (c) 2008-2012 CommonsWare, LLC
      Licensed under the Apache License, Version 2.0 (the "License"); you may not
      use this file except in compliance with the License. You may obtain a copy
      of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
      by applicable law or agreed to in writing, software distributed under the
      License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
      OF ANY KIND, either express or implied. See the License for the specific
      language governing permissions and limitations under the License.
    
      From _The Busy Coder's Guide to Android Development_
        http://commonsware.com/Android
    */
    
    package com.commonsware.android.appwidget.dice;
    
    import android.app.PendingIntent;
    import android.appwidget.AppWidgetManager;
    import android.appwidget.AppWidgetProvider;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.RemoteViews;
    
    public class AppWidget extends AppWidgetProvider {
      private static final int[] IMAGES={R.drawable.die_1,R.drawable.die_2,
                                          R.drawable.die_3,R.drawable.die_4,
                                          R.drawable.die_5,R.drawable.die_6};
    
      @Override
      public void onUpdate(Context ctxt, AppWidgetManager mgr,
                            int[] appWidgetIds) {
        ComponentName me=new ComponentName(ctxt, AppWidget.class);
    
        mgr.updateAppWidget(me, buildUpdate(ctxt, appWidgetIds));
      }
    
      private RemoteViews buildUpdate(Context ctxt, int[] appWidgetIds) {
        RemoteViews updateViews=new RemoteViews(ctxt.getPackageName(),
                                                R.layout.widget);
    
        Intent i=new Intent(ctxt, AppWidget.class);
    
        i.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
    
        PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0 , i,
                                                    PendingIntent.FLAG_UPDATE_CURRENT);
    
        updateViews.setImageViewResource(R.id.left_die,
                                         IMAGES[(int)(Math.random()*6)]); 
        updateViews.setOnClickPendingIntent(R.id.left_die, pi);
        updateViews.setImageViewResource(R.id.right_die,
                                         IMAGES[(int)(Math.random()*6)]); 
        updateViews.setOnClickPendingIntent(R.id.right_die, pi);
        updateViews.setOnClickPendingIntent(R.id.background, pi);
    
        return(updateViews);
      }
    }
    

    (来自this sample project

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多