如何使一个小部件Android的切换按钮 [英] How to make a toggle button for a widget android

查看:152
本文介绍了如何使一个小部件Android的切换按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我创建了一个小部件,而我尽我所能来模拟一个切换按钮。感觉小部件不支持改变依赖于它的国家的形象我使用的ImageButton和规划的切换按钮。

So I've created a widget, and I'm tried my best to simulate a toggle button. Sense Widget doesn't support a toggle button I'm using a imagebutton and plan on changing the image depending on it's state.

唯一的问题是它的状态不会被保存,我不知道为什么。

Only problem is it's state isn't being saved and I'm not sure why.

package com.tdubstudios.beastmode;

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;
import android.widget.Toast;

public class WidgetClass extends AppWidgetProvider{

private static final String ACTION_WIDGET_RECEIVER = "ActionRecieverWidget";  
private RemoteViews views;  

public boolean beastmodeOn = false;

public void onUpdate(Context context, AppWidgetManager appWidgetManager,  
       int[] appWidgetIds) {  
   final int N = appWidgetIds.length;
   views = new RemoteViews("com.tdubstudios.beastmode", R.layout.widget_layout);

   // Perform this loop procedure for each App Widget that belongs to this  
   // provider  
   for (int i = 0; i < N; i++) {  
       int appWidgetId = appWidgetIds[i];  
       Intent intent = new Intent(context, WidgetClass.class);  
       intent.setAction(ACTION_WIDGET_RECEIVER);  
       PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);  

       views.setOnClickPendingIntent(R.id.widget_ib, pendingIntent);  

       // Tell the AppWidgetManager to perform an update on the current App  
      // Widget  
       appWidgetManager.updateAppWidget(appWidgetId, views);  
   }
}  

@Override  
public void onReceive(Context context, Intent intent) {  
     if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) { 
         if(beastmodeOn){
             beastmodeOn = false;
             Toast.makeText(context, "beastmode is now off", Toast.LENGTH_SHORT).show();
         }else{
             beastmodeOn = true;
             Toast.makeText(context, "beastmode is now ON", Toast.LENGTH_SHORT).show();
         }
     }  
     super.onReceive(context, intent);  
}  

@Override
public void onDeleted(Context context, int[] appWidgetIds) {
    super.onDeleted(context, appWidgetIds);
    Toast.makeText(context, "onDelete has been called in widgetClass", Toast.LENGTH_LONG).show();
}

}

不管我多少次preSS的按钮,我总是得到敬酒beastmode是现在

No matter how many times I press the button I always get the toast "beastmode is now ON"

有什么建议?

感谢您。

编辑:

好了,所以我说这code:

Ok so I added this code:

Log.i("widget","BEFORE beastmode is: "+beastmodeOn);
             beastmodeOn = true;
             Toast.makeText(context, "beastmode is now ON", Toast.LENGTH_SHORT).show();
             Log.i("widget","AFTER beastmode is: "+beastmodeOn);

和我的日志给我这个回:

and my log gives me this back:

 BEFORE beastmode is: false
 AFTER beastmode is: true

这给了我这个我每次preSS的按钮。所以,很显然它会创建一个新的实例,或者诸如此类的话。一旦执行就必须销毁所有变量值什么的,也许有人有更多的知识知道正确的话。

It gives me this EVERY time I press the button. So obviously it creates a new instance or something to that effect. Once it executes it must destroy all variable values or something, maybe someone with more knowledge knows the right words.

因此​​,没有人知道周围的工作呢?

So does anyone know a work around then?

推荐答案

所以我不知道到底为什么上面没有工作,我假设,因为它会创建一个新的实例。我设法通过使用共享preferences跟踪切换,使周围虽然工作。也许不是最好的方式做到这一点,但它的作品,所以我很高兴。

So I don't exactly know why the above didn't work, I'm assuming because it creates a new instance. I've managed to make a work around though by using SharedPreferences to keep track of the toggle. Probably not the best way to do it but it works, so I'm happy.

有关其他人在这里有这个问题亚去:

For anyone else having this problem here ya go:

package com.tdubstudios.beastmode;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.widget.RemoteViews;
import android.widget.Toast;

public class WidgetClass extends AppWidgetProvider {

    private static final String ACTION_WIDGET_RECEIVER = "ActionRecieverWidget";
    private RemoteViews views;

    public boolean beastmodeOn;

    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        final int N = appWidgetIds.length;
        views = new RemoteViews("com.tdubstudios.beastmode",
                R.layout.widget_layout);

        // Perform this loop procedure for each App Widget that belongs to this
        // provider
        for (int i = 0; i < N; i++) {
            int appWidgetId = appWidgetIds[i];
            Intent intent = new Intent(context, WidgetClass.class);
            intent.setAction(ACTION_WIDGET_RECEIVER);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                    0, intent, 0);

            views.setOnClickPendingIntent(R.id.widget_ib, pendingIntent);

            SharedPreferences prefs = PreferenceManager
                    .getDefaultSharedPreferences(context);
            boolean value = prefs.getBoolean("beastmodeOn", false);

            if (value) {
                Editor editor = prefs.edit();
                editor.putBoolean("beastmodeOn", false);
                editor.commit();
            }

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {

            SharedPreferences prefs = PreferenceManager
                    .getDefaultSharedPreferences(context);
            boolean value = prefs.getBoolean("beastmodeOn", false);
            Editor editor = prefs.edit();

            if (value) {
                beastmodeOn = true;
            } else {
                beastmodeOn = false;
            }

            if (beastmodeOn) {
                Toast.makeText(context, "beastmode is now off",
                        Toast.LENGTH_SHORT).show();

                editor.putBoolean("beastmodeOn", false);
                editor.commit();
            } else {
                Toast.makeText(context, "beastmode is now ON",
                        Toast.LENGTH_SHORT).show();

                editor.putBoolean("beastmodeOn", true);
                editor.commit();
            }
        }
        super.onReceive(context, intent);
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        boolean value = prefs.getBoolean("beastmodeOn", false);

        if (value) {
            Editor editor = prefs.edit();
            editor.putBoolean("beastmodeOn", false);
            editor.commit();
        }

        super.onDeleted(context, appWidgetIds);
    }
}

这篇关于如何使一个小部件Android的切换按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆