【问题标题】:change icon when textview is clicked in widget在小部件中单击 textview 时更改图标
【发布时间】:2012-04-08 17:01:48
【问题描述】:

我在 android 中创建了一个小部件,每当我点击 Widget TextView 时,一项服务就会启动,但同时我想更改右侧显示的图标。

我可以使用

Widget TextView 上设置 onclick
remoteViews.setOnClickPendingIntent(R.id.widgetTxt,pendingIntent);

但我不知道当 TextView 获得点击时我可以在哪里更改图标

图片

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

                        // Here i am trying to set a onclick over TextView
    Intent intent = new Intent(context,MyService.class);
            PendingIntent pendingIntent = PendingIntent.getService(context,0 , 
                    intent, 0);

            remoteViews.setOnClickPendingIntent(R.id.MyWidgetTxt,pendingIntent);

    }

当一个图标本身被点击时,它也会发生变化,我已经在 onReceive 中设置了它的代码

【问题讨论】:

标签: android android-widget android-pendingintent android-2.2-froyo


【解决方案1】:

试试这个:

在 manifest.xml 中

 <receiver android:name="TestwidgetProvider"
            android:label="@string/widget_title" android:exported="false" android:icon="@drawable/test">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="com.imranandroid.demo.ACTION_ON_ImG_CLICK"/>
                <action android:name="android.appwidget.action.APPWIDGET_DELETED"/>
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />
        </receiver>

AppWidgetProvider.java 中:

  public class TestwidgetProvider extends AppWidgetProvider {
        public static final String TAG_IMAGE_CLICK = "com.imranandroid.demo.ACTION_ON_ImG_CLICK";
        public static RemoteViews rview;
public static int ii=0;
        @Override
        public void onUpdate(Context paramContext, AppWidgetManager appWidgetManager, 
                int[] appWidgetIds){
            //REMOVE YOUR CODE FROM HERE
            updateWidgetState(paramContext, "");    
        }
        @Override
        public void onReceive(Context paramContext, Intent paramIntent)
          {
             String str = paramIntent.getAction();
            if (paramIntent.getAction().equals(TAG_IMAGE_CLICK)) {
                updateWidgetState(paramContext, str);   
            }
            else
            {
              super.onReceive(paramContext, paramIntent);
            }
          }
         static void updateWidgetState(Context paramContext, String paramString)
          {
            RemoteViews localRemoteViews = buildUpdate(paramContext, paramString); //CALL HERE
            ComponentName localComponentName = new ComponentName(paramContext, TestwidgetProvider.class);
            AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews);
          }
     private static RemoteViews buildUpdate(Context paramContext, String paramString)
      {
         rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layoutmain);
         Intent active = new Intent(paramContext, TestwidgetProvider.class); //YOUR WIDGET NAME
         active.setAction(TAG_IMAGE_CLICK);
         PendingIntent actionPendingIntent = PendingIntent.getBroadcast(paramContext, 0, active, 0);
         rview.setOnClickPendingIntent(R.id.MyWidgetTxt, actionPendingIntent);
         if (paramString.equals(TAG_IMAGE_CLICK)) {
         if(ii==0)
            {
            rview.setImageViewResource(R.id.ImageView01a, R.drawable.off);
                    ii=1;
            }
            else
            {
            rview.setImageViewResource(R.id.ImageView01a, R.drawable.on);
                    ii=0;
            }
            }
         return rview;
      }
     }

【讨论】:

  • 如何调用 buildUpdate?
  • 看到这个答案:-[小部件被点击](stackoverflow.com/questions/9671596/…)
  • 如果你从你的小部件中分享一些AppWidgetProvider 的代码,那么我将为你编辑,因为这取决于你如何设计你的小部件。
  • 你必须在清单文件中注册一个图片点击接收器
  • ur 使用 TestwidgetProvider 类,该类本身接收清单文件中定义的消息,但是当单击文本视图时,我也想同时运行服务,这就是为什么我使用 MyService.class 而不是 TestWidgetProvider。类本身,明白我的意思吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 2022-10-05
相关资源
最近更新 更多