1 SharedPreferences 介绍
SharedPreferences是使用键值对的方式来存储数据的
SharedPreferences share = getSharedPreferences(\"my_file\", Context.MODE_PRIVATE); SharedPreferences.Editor editor = share.edit(); // 4 保存数据到文件 editor.putString(\"account\", input_account.getText().toString()); editor.putString(\"password\", input_password.getText().toString()); editor.putBoolean(\"pass_remem\", pass_remem.isChecked()); // 单选框 选中时返回为 true
当保存一条数据的时候,需要给这条数据提供一个对应的键,可以通过这个键把相应的值取出来
SharedPreferences sharedPreferences = getSharedPreferences(\"my_file\", Context.MODE_PRIVATE); Boolean pass_remem_ = sharedPreferences.getBoolean(\"pass_remem\", false);
它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用SharedPreferences保存数据,文件存放在/data/data/<package name>/shared_prefs
目录下
1.1 SharedPreferences 四种操作模式
- Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
- Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
- MODE_WORLD_READABLE:表示当前文件可以被其他应用读取.
- MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入.
1.3 使用方法
由于SharedPreferences是一个接口,而且在这个接口里没有提供写入数据和读取数据的能力。但其内部有一个Editor内部接口,Editor接口有一系列方法来操作SharedPreference
1.edit( ) 获得SharedPreferences.Edit对象 getSharedPreferences(\"myfile\",0).edit( )
2.向对象中添加数据
- putString( )
- putInt( )
- putBoolean( )
editor.putString(“name”, “张三\"); editor.putInt(“age”, 21); editor.putBoolean(\"married\",true)
3.commit( ) 提交数据,完成数据存储操作 editor.commit( );
4.从文件中读取数据 第一个参数为KEY 第二个参数为访问失败时的默认值
- getString( )
- getInt( )
- getBoolean( )
getString (\"name\", \"\"); getInt (“age\", 0); getBoolean (“married\", false);
2 使用 SharedPreferences 进行登录
2.1 前端设计
<?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\"> <TextView android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\" android:text=\"@string/app_name\" android:textSize=\"36sp\" android:gravity=\"center\" android:layout_marginTop=\"100dp\" android:layout_marginBottom=\"10dp\" /> <LinearLayout android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\" android:orientation=\"horizontal\"> <TextView android:id=\"@+id/account\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:layout_weight=\"1\" android:text=\"@string/S_account\" android:gravity=\"center\" android:textSize=\"16sp\" /> <EditText android:id=\"@+id/input_account\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:layout_weight=\"2\" android:ems=\"10\" android:textSize=\"16sp\" android:paddingLeft=\"10dp\" android:inputType=\"textPersonName\" android:hint=\"@string/S_input_account\"/> </LinearLayout> <LinearLayout android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\" android:orientation=\"horizontal\"> <TextView android:id=\"@+id/password\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:layout_weight=\"1\" android:text=\"@string/S_password\" android:gravity=\"center\" android:textSize=\"16sp\" /> <EditText android:id=\"@+id/input_password\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:layout_weight=\"2\" android:ems=\"10\" android:textSize=\"16sp\" android:paddingLeft=\"10dp\" android:inputType=\"numberPassword\" android:hint=\"@string/S_input_password\"/> </LinearLayout> <CheckBox android:id=\"@+id/password_remember\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:textSize=\"16sp\" android:layout_gravity=\"right\" android:layout_marginRight=\"30dp\" android:layout_marginBottom=\"33dp\" android:text=\"@string/S_pass_remem\" android:checked=\"false\" /> <Button android:id=\"@+id/submit\" android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\" android:padding=\"5dp\" android:layout_margin=\"20dp\" android:text=\"@string/S_button_submit\" android:textSize=\"24sp\" /> </LinearLayout>
2.1 Control层
public class MainActivity extends AppCompatActivity { private EditText input_account, input_password; private CheckBox pass_remem; private Button submit_button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 1 获取各个组件的信息, 并存储到数据层 input_account = this.findViewById(R.id.input_account); input_password = this.findViewById(R.id.input_password); pass_remem = this.findViewById(R.id.password_remember); submit_button = this.findViewById(R.id.submit); // 2 设置按钮的点击事件 submit_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 3 获取SharedPreferences SharedPreferences share = getSharedPreferences(\"my_file\", Context.MODE_PRIVATE); SharedPreferences.Editor editor = share.edit(); // 4 保存数据到文件 editor.putString(\"account\", input_account.getText().toString()); editor.putString(\"password\", input_password.getText().toString()); editor.putBoolean(\"pass_remem\", pass_remem.isChecked()); // 单选框 选中时返回为 true // 5 提交数据, 并进行提示 editor.commit(); Toast.makeText(MainActivity.this, \"数据写入成功\", Toast.LENGTH_SHORT).show(); // App条状 Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent); } }); // 6 如果选中,下一次加载数据 SharedPreferences sharedPreferences = getSharedPreferences(\"my_file\", Context.MODE_PRIVATE); Boolean pass_remem_ = sharedPreferences.getBoolean(\"pass_remem\", false); if (pass_remem_) { String account = sharedPreferences.getString(\"account\", \"\"); String password = sharedPreferences.getString(\"password\", \"\"); input_account.setText(account); input_password.setText(password); pass_remem.setChecked(pass_remem_); // 恢复到原来的状态 } } }
到此这篇关于Android 使用 SharedPreferences 保存少量数据的文章就介绍到这了,更多相关Android 保存数据内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!