1.SharedPreferences概述
- 使用键值对的方式来存储数据。
- 支持多种不同的数据类型的存储
(非常适合用来存储一些简单的配置信息)
2.如何获取SharedPreferences对象呢?
方式一:Context类中的getSharedPreferences()方法
该方法接受两个参数:
第一个参数指定文件的名称,若指定文件不存在则创建一个
第二个参数指定操作模式(MODE_PRIVATE、MODE_APPEND、MODE_WORLD_READABLE、MODE_WORLD_WRITEABLE)
方式二:Activity类中的getPreferences()方法
该方法只接受一个操作模式参数,因为使用这个方法时会自动将当前活动的类名作为SharedPreferences的文件名。
方式三:PreferenceManager类中的getDefaultSharedPreferences()方法。
该方法是一个静态方法,它接受一个Context参数,并自动使用当前程序的包名作为前缀来命名
3。
4。
5。登录案例
案例结果图:
代码如下:
布局文件(loginlayout.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/drawable_rounder"
android:padding="@dimen/activity_horizontal_margin">
<EditText
android:id="@+id/EtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/etname"
android:ems="10"
android:drawablePadding="10dp"
android:background="@android:drawable/edit_text"
android:drawableLeft="@drawable/icon_user" >
<requestFocus/>
</EditText>
<EditText
android:id="@+id/EtPass"
android:layout_below="@id/EtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/etpass"
android:drawablePadding="10dp"
android:inputType="textPassword"
android:ems="10"
android:background="@android:drawable/edit_text"
android:drawableLeft="@drawable/icon_pass">
<requestFocus/>
</EditText>
<CheckBox
android:id="@+id/cbpass"
android:layout_below="@id/EtPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:textSize="20dp"
android:layout_margin="5dp"
android:checked="false"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/cbpass">
<Button
android:id="@+id/btn"
android:onClick="login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/de"
android:layout_marginRight="10dp"
android:background="@drawable/bt_press"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/zhuce"
android:background="@drawable/bt_press"
/>
</LinearLayout>
</RelativeLayout>
(activity_login.xml)布局文件代码如下:
<?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:background="@drawable/loginbg"
android:id="@+id/activity_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="bzu.edu.cn.case_login.LoginActivity">
<include layout="@layout/loginlayout"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/deer"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="23dp"
android:layout_marginEnd="23dp"
android:id="@+id/imageView" />
</RelativeLayout>
对布局背景的设置在drawable下新建loginbg.xml和loginbg_round.xml代码分别如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#FFACDAE5"
android:endColor="#FF72CAE1"
android:angle="45"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="10dp"></corners>
<solid android:color="#55FFFFFF"></solid>
</shape>
对按钮进行设置:在drawable下分别新建btn_press.xml、btn_shape.xml、btn_shape_after.xml,代码分别如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btu_shape" android:state_pressed="false"></item>
<item android:drawable="@drawable/btu_shape_after" android:state_pressed="true"></item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dp"></corners>
<solid android:color="#FF72CAE1"></solid>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dp"></corners>
<solid android:color="#82cefa"></solid>
</shape>
loginActivity.java
package bzu.edu.cn.case_login;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity {
private EditText Ename,Epass;
private CheckBox cb;
private Button btn;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//获取控件
Ename=(EditText)findViewById(R.id.EtName);
Epass=(EditText)findViewById(R.id.EtPass);
cb=(CheckBox)findViewById(R.id.cbpass);
//通过getSharedPreferences()方法获取SharePreferences的实例对象
sharedPreferences=getSharedPreferences("Login",MODE_PRIVATE);
//获取Editor实例对象,存储数据
editor=sharedPreferences.edit();
//获取数据
String name=sharedPreferences.getString("name","");
String pass=sharedPreferences.getString("pass","");
//判断文本框是否为空
if(name==null&&pass==null){
cb.setChecked(false);
}
else{
cb.setChecked(true);
Ename.setText(name);
Epass.setText(pass);
}
}
public void login(View view){
//获取文本框的内容
String name = Ename.getText().toString().trim();
String pass=Epass.getText().toString().trim();
//检验用户名和密码是否正确
if("admin".equals(name)&&"123456".equals(pass)){
if(cb.isChecked()){
//保存数据
editor.putString("name",name);
editor.putString("pass",pass);
editor.commit(); //提交数据
}else{
editor.clear(); //删除所有数据
editor.commit(); //提交修改
}
Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this,"用户名或密码错误",Toast.LENGTH_LONG).show();
}
}
}