SharedPreferences 讲解与实例应用(登录界面)

SharedPreferences :

是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置

Sharedpreferences提供的数据类型:

常规的数据类型,保存接口比如:int、long、boolean、String、Float、Set和Map这些数据类型

存贮位置:

/data/data/包名/share_pref

保存数据类型:

以xml数据形式保存

使用SharedPreferences读写数据具体步骤:

一、使用SharedPreferences写入数据:

1.获得SharesPreferences对象(参数为(文件名,存贮模式))
SharedPreferences sp = getSharedPreferences("user.xml",MODE_PRIVATE);
2.获得Editor对象:
SharedPreferences.Editor editor = sp.edit();
3.通过editor对象写入数据:
String username = nameEt.getText().toString();
editor.putString("username",username);
4.提交数据
 editor.commit();

二、使用SharedPreferences读取数据

1.获得SharedPreferences对象:
SharedPreferences sp2 = getSharedPreferences("user.xml",MODE_PRIVATE);
2.通过getxxx,获得数据
String name = sp2.getString("username","");
            nameEt.setText(name);

使用SharedPreferences实例(登录界面实现记住账户名)

效果展现:

1.修改layout布局,添加控件,注意绑定id
<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.myapplication2.LoginActivity">


    <EditText
        android:id="@+id/login_name_et"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <EditText
        android:inputType="textPassword"
        android:id="@+id/login_pass_et"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <Button
        android:id="@+id/login_loginBtn"
        android:layout_width="match_parent"
        android:text="登录"
        android:layout_height="50dp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <CheckBox
            android:id="@+id/login_pw_cb"
            android:layout_centerVertical="true"
            android:layout_width="30dp"
            android:layout_height="20dp" />
        <TextView
            android:layout_toRightOf="@+id/login_pw_cb"
            android:layout_width="wrap_content"
            android:textSize="20sp"
            android:gravity="center"
            android:text="记住密码"
            android:layout_height="match_parent" />

    </RelativeLayout>





</LinearLayout>
2、在activity界面绑定id,设置监听,给登录键设置监听,写入数据,在onCreate()方法中读数据
public class LoginActivity extends AppCompatActivity {

    private EditText nameEt;
    private EditText pwEt;
    private Button loginBtn;
    private CheckBox pwCbox;
    //创建flag用处是判断上一次登录时是否点击了checkbox;
    private int pwFlag = 0;
    private String pw = null;


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

        bangID();
        //创建sharedpreferences对象
        SharedPreferences sp2 = getSharedPreferences("user.xml",MODE_PRIVATE);
        if(sp2!=null){
            //获得存贮的数据
            String name = sp2.getString("username","");
            pw = sp2.getString("pw","");
            //更新UI
            nameEt.setText(name);
            pwFlag = sp2.getInt("pwFlag",0);

        }
        //更具读取的数据,判断上一次是否点击了CheckBox
        if(pwFlag==1){
            pwCbox.setChecked(true);
            pwEt.setText(pw);
        }



        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建sharedpreferencees对象
                SharedPreferences sp = getSharedPreferences("user.xml",MODE_PRIVATE);
                //获得editor对象
                SharedPreferences.Editor editor = sp.edit();
                String username = nameEt.getText().toString();
                pw = pwEt.getText().toString();
                //将获得的数据写入到文本中
                editor.putString("username",username);
                editor.putString("pw",pw);
                //判断当前页面是否是否点击了CheckBox
                if(pwCbox.isChecked()){
                    pwFlag = 1;
                }else{
                    pwFlag = 0;
                }
                editor.putInt("pwFlag",pwFlag);
                //提交数据
                editor.commit();
                Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();

            }
        });
    }

    private void bangID() {
        nameEt = findViewById(R.id.login_name_et);
        pwEt = findViewById(R.id.login_pass_et);
        pwCbox = findViewById(R.id.login_pw_cb);
        loginBtn = findViewById(R.id.login_loginBtn);
    }
}

猜你喜欢

转载自blog.csdn.net/shaochen2015821426/article/details/79613340