您的位置:首页 > 移动开发 > Android开发

Android课堂学习笔记——SharedPreferences

2018-03-19 16:28 399 查看

什么是SharedPreferences

SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedPereferences中;当Activity重载,系统回调方法onSaveInstanceState时,再从SharedPreferences中将值取出。

SharedPreferences用法

1.用getSharedPreferences方法获得SharedPreferences对象

2.调用SharedPreferences对象的edit()方法来获取一个SharedPreferences.Editor对象

3.通过SharedPreferences.Editor对象的putXXX函数,设置写入数据

4.调用commit方法将添加的数据提交

登录界面记住密码操作效果展示



登录界面记住密码操作代码展示

xml代码

<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="com.example.weather.DengluActivity">

<EditText
android:id="@+id/zhanghao_et"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="输入账号"/>

<EditText
android:id="@+id/mima_et"
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="textPassword"
android:hint="输入密码"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<CheckBox
android:id="@+id/mima_cb"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="记住密码"/>

<CheckBox
android:id="@+id/denglu_cb"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="自动登录"/>

</LinearLayout>

<Button
android:id="@+id/denglu_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="登录"
android:textSize="25sp"/>

</LinearLayout>


java代码

public class DengluActivity extends AppCompatActivity {

private EditText zhanghaoEt;
private EditText mimaEt;
private CheckBox mimaCb;
private CheckBox dengluCb;
private Button dengluBtn;

private int rememberFlag = 0;
private String mima;

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

bind();

//从sp文件(mysp.xml)里取出“name”节点对应的值
SharedPreferences sharedPreferences = getSharedPreferences("mysp.xml",MODE_PRIVATE);
if (sharedPreferences!=null){
String name = sharedPreferences.getString("name","");
mima = sharedPreferences.getString("mima","");
//赋值给zhanghaoEt
zhanghaoEt.setText(name);

rememberFlag = sharedPreferences.getInt("remember_frag",0);
}

if (rememberFlag==1){
mimaCb.setChecked(true);
mimaEt.setText(mima);
}

dengluBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String zhanghao = zhanghaoEt.getText().toString();
String mima = mimaEt.getText().toString();

//1.创建SharesPreferences对象
SharedPreferences spf = getSharedPreferences("mysp.xml", Context.MODE_PRIVATE);
//2.创建Editioon对象,写入值
SharedPreferences.Editor editor = spf.edit();
editor.putString("name",zhanghao);

if (mimaCb.isChecked()){
rememberFlag = 1;
editor.putInt("remember_frag",rememberFlag);
editor.putString("mima",mima);
}else {
rememberFlag = 0;
editor.putInt("remember_frag",rememberFlag);
}

//3.提交
editor.commit();

Toast.makeText(DengluActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
}
});

}

private void bind() {
zhanghaoEt = findViewById(R.id.zhanghao_et);
mimaCb = findViewById(R.id.mima_cb);
mimaEt = findViewById(R.id.mima_et);
dengluCb = findViewById(R.id.denglu_cb);
dengluBtn  = findViewById(R.id.denglu_btn);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: