sp存储和读取,保存登录信息练习

package com.hanqi.testapp111;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class denglu extends AppCompatActivity {
    EditText et_1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_denglu);
        et_1=(EditText)findViewById(R.id.et_1);
        SharedPreferences sp=getSharedPreferences("a",MODE_PRIVATE);
        String str =sp.getString("a",null) ;
        et_1.setText(str);

    }
    public void bt_OnClick(View v)
    {
        String s1=et_1.getText().toString();
        SharedPreferences sharedPreferences=getSharedPreferences("baocun",MODE_APPEND);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString("a",s1);
        editor.commit();
        Toast.makeText(denglu.this, "保存数据成功", Toast.LENGTH_SHORT).show();

    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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="com.hanqi.testapp111.denglu"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="请输入用户名"/>
    <EditText
            android:layout_width="match_parent"
           android:layout_height="wrap_content"
            android:textSize="20dp"
            android:id="@+id/et_1"/>
    </LinearLayout>
           <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="登录"
                android:onClick="bt_OnClick"/>

原文地址:https://www.cnblogs.com/jiang2538406936/p/5523391.html