从共享首选项打开/关闭Toast?

[英]Turn Toast on / off from shared preferences?


Is there a way to globally turn on and off toast notifications with a checkbox in shared preferences?

有没有办法通过共享偏好设置中的复选框全局打开和关闭Toast通知?

PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
        boolean showToast = myPrefs.getBoolean("showToast",
                true);

I was thinking maybe making a class:

我在想也许上课:

boolean showToast(){
 //code
}

BUT thought, SO might have a global solution?

但是想想,SO可能有全球解决方案吗?

Should I use a different type of notification system?

我应该使用不同类型的通知系统吗?

Any thoughts?

5 个解决方案

#1


1  

You may consider extending Toast to create your custom class which is smart enough to read the user preferences before showing the toast.

您可以考虑扩展Toast以创建自定义类,该类足够智能以在显示toast之前读取用户首选项。

Then refactor your code to replace Toast with SmartToast.

然后重构您的代码以使用SmartToast替换Toast。

SmartToast.makeText(this, "message", Toast.LENGTH_SHORT).show();

SmartToast.makeText(this,“message”,Toast.LENGTH_SHORT).show();

So implement SmartToast.makeText() to return an instance of SmartToast and override show() as follows:

因此,实现SmartToast.makeText()以返回SmartToast的实例并覆盖show(),如下所示:

@Override
public void show() {
    if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean("showToast", true)) {
        super.show();
    }
}

#2


0  

Why don't you put the checkbox value in the shared preferences and then use is as a flag, If(flag) { show toast} for example, isn't this the same?

为什么不将复选框值放在共享首选项中然后使用is作为标志,If(flag){show toast}例如,这不是一样的吗?

#3


0  

You can only disable or enable toast notifications for your appplication like the way u r doing it...but not globally for all applications

您只能为您的应用程序禁用或启用Toast通知,就像您执行此操作一样...但不是全局适用于所有应用程序

#4


0  

You can try creating a enum with one element -

您可以尝试使用一个元素创建枚举 -

public enum YourSingleton {
INSTANCE;    // element in this enum

public static void makeToast(Context context, String msg) {
    Toast toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
    toast.show();
}

} And then to call -

然后打电话 -

 YourSingleton .makeToast(this, "Toast Message");

When you want to turn on or off - you can simply do that in the makeToast() method.

当您想要打开或关闭时 - 您可以在makeToast()方法中执行此操作。

#5


0  

use onSharedPreferenceChangeListener.

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/03/03/72030151a6794844fbf4caab65864c1f.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告