赞
踩
实现的效果如图所示
1.主活动MainActivity类。代码内容如下
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { Button btn_1,btn_2; TextView tv_nr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_nr = (TextView) findViewById(R.id.tv_nr); btn_1 = (Button) findViewById(R.id.btn_1); btn_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SpUtils.getInstance().putString("setting","我正在设置"); } }); btn_2 = (Button) findViewById(R.id.btn_2); btn_2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { tv_nr.setText(SpUtils.getInstance().getString("setting")); } }); } }
2.主活动MainActivity类的activity_main布局代码如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="sp存数据" /> <Button android:id="@+id/btn_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="sp取数据" /> </LinearLayout> <TextView android:id="@+id/tv_nr" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center" android:textColor="@color/black" android:textSize="24sp" /> </LinearLayout>
3.SharedPreferences的工具类SpUtils。代码内容如下
import android.content.Context; import android.content.SharedPreferences; public class SpUtils { private final static String SP_NAME = "setting"; private SharedPreferences sharedPreferences = null; private static SpUtils spUtils = null; public static SpUtils getInstance() { if (spUtils == null) { return newInstance(); } return spUtils; } private synchronized static SpUtils newInstance() { if (spUtils == null) { spUtils = new SpUtils(); } return spUtils; } public SharedPreferences.Editor getEditor() { return sharedPreferences.edit(); } //sp存数据 public void putString(String key, String value) { getEditor().putString(key, value).apply(); } //sp取数据 public String getString(String key) { return sharedPreferences.getString(key, "暂无保存的数据"); //"暂无保存的数据"为该key为空时的默认返回值 } public int getInt(String key) { return sharedPreferences.getInt(key, 0); } public Long getLong(String key) { return sharedPreferences.getLong(key, 0); } public boolean getBoolean(String key) { return sharedPreferences.getBoolean(key, false); } private SpUtils() { sharedPreferences = App.getInstance().getSharedPreferences(SP_NAME, Context.MODE_PRIVATE); } }
4.App继承Application类的启动类。代码内容如下
import android.app.Application; import android.content.Context; import android.content.res.Configuration; import android.util.Log; public class App extends Application { static Context context; public static Context getInstance() { return context; } /* Aplication创建时被调用,可以在该方法里进行一些初始化操作 */ @Override public void onCreate() { super.onCreate(); Log.d("111333","Aplication创建被调用"); context=getApplicationContext(); } /* 系统配置发生变更 的时候被调用 如:屏幕方向更改 、系统语言更改 */ @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); } /* 系统内存吃紧时被调用,用于释放内存 */ @Override public void onLowMemory() { super.onLowMemory(); } }
需要在AndroidManifest.xml文件中进行全局类配置。不配置是不会启动自定义的全局类的。完整的代码内容如下
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" > <application android:name=".App" android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyApplication002" tools:targetApi="31" > <activity android:name=".MainActivity" android:exported="true" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="android.app.lib_name" android:value="" /> </activity> </application> </manifest>
需要配置的是这个地方,请看下图
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。