gpt4 book ai didi

java - 创建可在整个android项目中使用的Settings类

转载 作者:行者123 更新时间:2023-12-02 13:04:45 25 4
gpt4 key购买 nike

下面的代码是我为 Windows Phone 应用程序编写的设置类。它非常容易使用:

using System;
using System.IO.IsolatedStorage;
using System.Diagnostics;

namespace myAppName
{
public class AppSettings
{

// Our isolated storage settings
IsolatedStorageSettings settings;

// The isolated storage key names of our settings
const string InstalledonSettingKeyName = "InstalledonSetting";
const string SeenTutorialSettingKeyName = "SeenTutorialSetting";
const string FontSizeSettingKeyName = "FontSizeSetting";
const string FontTypeSettingKeyName = "FontTypeSetting";

// The default value of our settings
const long InstalledonSettingDefault = 0;
const bool SeenTutorialSettingDefault = false;
const int FontSizeSettingDefault = 30;
const string FontTypeSettingDefault = "";

/// <summary>
/// Constructor that gets the application settings.
/// </summary>
public AppSettings()
{
try
{
// Get the settings for this application.
settings = IsolatedStorageSettings.ApplicationSettings;

}
catch (Exception e)
{
Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
}
}

/// <summary>
/// Update a setting value for our application. If the setting does not
/// exist, then add the setting.
/// </summary>
/// <param name="Key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool AddOrUpdateValue(string Key, Object value)
{
bool valueChanged = false;

// If the key exists
if (settings.Contains(Key))
{
// If the value has changed
if (settings[Key] != value)
{
// Store the new value
settings[Key] = value;
valueChanged = true;
}
}
// Otherwise create the key.
else
{
settings.Add(Key, value);
valueChanged = true;
}

return valueChanged;
}


/// <summary>
/// Get the current value of the setting, or if it is not found, set the
/// setting to the default setting.
/// </summary>
/// <typeparam name="valueType"></typeparam>
/// <param name="Key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
{
valueType value;

// If the key exists, retrieve the value.
if (settings.Contains(Key))
{
value = (valueType)settings[Key];
}
// Otherwise, use the default value.
else
{
value = defaultValue;
}

return value;
}


/// <summary>
/// Save the settings.
/// </summary>
public void Save()
{
settings.Save();
}

public long InstalledonSetting
{
get
{
return GetValueOrDefault<long>(InstalledonSettingKeyName, InstalledonSettingDefault);
}
set
{
if (AddOrUpdateValue(InstalledonSettingKeyName, value))
{
Save();
}
}
}

public int FontSizeSetting
{
get
{
return GetValueOrDefault<int>(FontSizeSettingKeyName, FontSizeSettingDefault);
}
set
{
if (AddOrUpdateValue(FontSizeSettingKeyName, value))
{
Save();
}
}
}

public string FontTypeSetting
{
get
{
return GetValueOrDefault<string>(FontTypeSettingKeyName, FontTypeSettingDefault);
}
set
{
if (AddOrUpdateValue(FontTypeSettingKeyName, value))
{
Save();
}
}
}

public bool SeenTutorialSetting
{
get
{
return GetValueOrDefault<bool>(SeenTutorialSettingKeyName, SeenTutorialSettingDefault);
}
set
{
if (AddOrUpdateValue(SeenTutorialSettingKeyName, value))
{
Save();
}
}
}
}

}

现在在另一个类中使用这个类非常容易

像这样

namespace myAppName
{
public partial class MyActivity : PhoneApplicationPage
{
private AppSettings settings = new AppSettings();
public MyActivity()
{
InitializeComponent();

if (!settings.SeenTutorialSetting) ShowTutorial();
}

}

现在我关心的是我可以对 Android java 类做同样的事情吗:

package com.example.myappname;

public class AppSettings{
String InstalledonSettingKeyName = "InstalledonSetting";
String SeenTutorialSettingKeyName = "SeenTutorialSetting";
String FontSizeSettingKeyName = "FontSizeSetting";
String FontTypeSettingKeyName = "FontTypeSetting";

// The default value of our settings
ong InstalledonSettingDefault = 0;
bool SeenTutorialSettingDefault = false;
int FontSizeSettingDefault = 30;
String FontTypeSettingDefault = "";

public AppSettings(){}

我不喜欢这样做:

if (!PreferenceManager.getDefaultSharedPreferences(this).getBoolean("myapp_finished_loading", false)) 
{
}

甚至是这个:

SharedPreferences vSettings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
long used_time_l = System.currentTimeMillis() - vSettings.getLong("myapp_first_data", 0);

任何有关 android java 类继承的帮助将不胜感激。如果您必须投反对票,请告诉我原因。

最佳答案

您可以在创建对象时初始化首选项并稍后使用它

class Settings {

public static String PARAM_PARAM1 = "param1";
public static String PARAM_PARAM2 = "param2";

private static String DEFAULT_PARAM1 = "hello";
private static boolean DEFAULT_PARAM2 = false;

private SharedPreferences prefs;

public Settings(Context ctx){
prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
}

public String getParam1(){
return prefs.getString(PARAM_PARAM1, DEFAULT_PARAM1);
}

public void setParam1(String param1){
prefs.edit()
.putString(PARAM_PARAM1, param1)
.apply();
}

public boolean getParam2(){
return prefs.getBoolean(PARAM_PARAM1, DEFAULT_PARAM2);
}

public void setParam2(boolean param2){
prefs.edit()
.putBoolean(PARAM_PARAM2, param2)
.apply();
}
}

关于java - 创建可在整个android项目中使用的Settings类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44203690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com