gpt4 book ai didi

java - 第一次尝试尝试共同的偏好

转载 作者:行者123 更新时间:2023-12-02 03:55:41 28 4
gpt4 key购买 nike

public class MainActivity extends Activity {
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = (TextView) findViewById(R.id.textView);

SharedPreferences sp = this.getPreferences( Context.MODE_PRIVATE );
SharedPreferences.Editor editor = sp.edit();

Random rand = new Random();

int randomNum = rand.nextInt( 1000 );

editor.putString("EQUATION_KEY_" + String.valueOf(randomNum), "");
editor.commit();

String str = sp.getString(String.valueOf(randomNum), "");

textView.setText(str);
}
}

我不知道为什么这不能仅在屏幕上显示保存的文本。 logcat 是这样说的:

02-18 10:11:24.988 3196-3196/? I/art: Not late-enabling -Xcheck:jni (already on)
02-18 10:11:25.169 3196-3196/com.example.luke.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.luke.myapplication-2/lib/x86
02-18 10:11:25.252 3196-3217/com.example.luke.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-18 10:11:25.430 3196-3217/com.example.luke.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
02-18 10:11:25.749 3196-3217/com.example.luke.myapplication W/EGL_emulation: eglSurfaceAttrib not implemented
02-18 10:11:25.750 3196-3217/com.example.luke.myapplication W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xab78ee40, error=EGL_SUCCESS
02-18 10:11:38.677 3196-3202/com.example.luke.myapplication W/art: Suspending all threads took: 21.161ms

请帮助我,我是菜鸟,但想学习。

最佳答案

为您的应用程序创建通用类,请按照以下步骤操作:

第 1 步:创建应用程序级别类

public class AppConfig extends Application {

private static AppConfig appInstance;
private static SharedPreferences sharedPreferences;
private static SharedPreferences.Editor sharedPreferencesEditor;
private static Context mContext;

@Override
public void onCreate()
{
super.onCreate();
appInstance = this;
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
sharedPreferencesEditor = sharedPreferences.edit();
setContext(getApplicationContext());

}

public static Context getContext() {
return mContext;
}

public static void setContext(Context mctx) {
mContext = mctx;
}


public static AppConfig getAppInstance() {
if (appInstance == null)
throw new IllegalStateException("The application is not created yet!");
return appInstance;
}

/**
* Application level preference work.
*/
public static void preferencePutInteger(String key, int value) {
sharedPreferencesEditor.putInt(key, value);
sharedPreferencesEditor.commit();
}

public static int preferenceGetInteger(String key, int defaultValue) {
return sharedPreferences.getInt(key, defaultValue);
}

public static void preferencePutBoolean(String key, boolean value) {
sharedPreferencesEditor.putBoolean(key, value);
sharedPreferencesEditor.commit();
}

public static boolean preferenceGetBoolean(String key, boolean defaultValue) {
return sharedPreferences.getBoolean(key, defaultValue);
}

public static void preferencePutString(String key, String value) {
sharedPreferencesEditor.putString(key, value);
sharedPreferencesEditor.commit();
}

public static String preferenceGetString(String key, String defaultValue) {
return sharedPreferences.getString(key, defaultValue);
}

public static void preferencePutLong(String key, long value) {
sharedPreferencesEditor.putLong(key, value);
sharedPreferencesEditor.commit();
}

public static long preferenceGetLong(String key, long defaultValue) {
return sharedPreferences.getLong(key, defaultValue);
}

public static void preferenceRemoveKey(String key) {
sharedPreferencesEditor.remove(key);
sharedPreferencesEditor.commit();
}

public static void clearPreference() {
sharedPreferencesEditor.clear();
sharedPreferencesEditor.commit();
}

}

第 2 步:将此类定义到 Application 标记中的 Manifest.xml 中,如下所示

<application
android:name=".AppConfig">
</application>

第 3 步:您可以在您的 Activity 中使用以下代码

AppConfig.preferencePutInteger("Key",Value);
AppConfig.preferenceGetInteger("Key", 0)

完成快乐编码......:)我希望你理解。

关于java - 第一次尝试尝试共同的偏好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35489346/

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