從共享首選項打開/關閉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.


注意!

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



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