版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sup_chao/article/details/79838064
Android攻城狮数据篇—SharedPreference
Android的四种数据存储方式:
SharedPreference
SQLite
Content Provider
File
SharedPreferences简介
SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。
实现Shared Preferences存储的步骤如下:
(1)使用Activity类的getSharedPreferences方法获得
SharedPreferences对象,其中存储key-value的文件的名称由getSharedPreferences方法的第一个参数指定。
(2)使用SharedPreferences接口的edit获得SharedPreferences.Editor对象。
(3)通过SharedPreferences.Editor接口的putXxx方法保存key-value对。其中XXX表示不同的数据类型。例如: 字符串类型的value需要用putString方法。
(4)通过SharedPreferences.Edior接口的commit方法保存key-value对。commit方法相当于数据库事务中的提交(commit )操作。
具体实现代码
public class MainActivity extends AppCompatActivity {
EditText put_name,put_word;
CheckBox checkBox;
SharedPreferences preferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
put_name = findViewById(R.id.put_name);
put_word = findViewById(R.id.put_word);
checkBox = findViewById(R.id.check);
preferences = getSharedPreferences("myPref",MODE_PRIVATE);
editor = preferences.edit();
String name = preferences.getString("username","");
if(name == null){
checkBox.setChecked(false);
}else{
checkBox.setChecked(true);
put_name.setText(name);
}
}
public void DoClick(View view){
switch (view.getId()){
case R.id.login:
String name = put_name.getText().toString().trim();
String words = put_word.getText().toString().trim();
if("admin".equals(name) &&"123456".equals(words)){
if( checkBox.isChecked()){
editor.putString("username", name);
editor.putString("password",words);
editor.commit();
}else{
editor.remove("username");
editor.commit();
}
Toast.makeText(MainActivity.this, "登陆成功",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "禁止登陆",Toast.LENGTH_SHORT).show();
}
break;;
default:
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="e.a17909.myapplication.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/username"
android:layout_toRightOf="@+id/username"
android:id="@+id/put_name" />
<EditText
android:id="@+id/put_word"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/put_name"
android:layout_toEndOf="@+id/password"
android:layout_toRightOf="@+id/password" />
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/put_name"
android:layout_alignBottom="@+id/put_name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="用户名" />
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/put_word"
android:layout_alignBottom="@+id/put_word"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/put_name"
android:layout_toStartOf="@+id/put_name"
android:text="密 码" />
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/put_word"
android:text="记住用户名" />
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="DoClick"
android:layout_below="@+id/check"
android:text="登陆" />
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/login"
android:layout_alignBottom="@+id/login"
android:layout_toEndOf="@+id/login"
android:layout_toRightOf="@+id/login"
android:text="取消" />
</RelativeLayout>