SharedPreferences方法的基本使用

通过SharedPreferences方法创建.xml文件,存储数据
具体用法如下:
 
 
package com.example.administrator.jreduch08;

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

public class SharedPreferencesActivity extends AppCompatActivity {
    private EditText user;
    private EditText pwd;
    private Button save;
    private CheckBox checkBox;
    private CheckBox checkBox2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        user=(EditText)findViewById(R.id.user);
        pwd=(EditText)findViewById(R.id.pwd);
        save=(Button)findViewById(R.id.save);
        checkBox=(CheckBox)findViewById(R.id.checkBox);
        checkBox2=(CheckBox)findViewById(R.id.checkBox2);
        //创建.xml文件
        final SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               //可以记忆数据
               //编辑数据
                SharedPreferences.Editor editor=sp.edit();
                editor.putString("userName", user.getText().toString());
                //把String型转化成Int型
                editor.putInt("userPwd", Integer.parseInt(pwd.getText().toString()));
                editor.commit();
                //跳转
                Intent intent=new Intent(SharedPreferencesActivity.this,Sp2Activity.class);
                startActivity(intent);

 

  });
    }
}

 

跳转到

package com.example.administrator.jreduch08;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        Button button=(Button)findViewById(R.id.button);
        Switch switch1=(Switch)findViewById(R.id.switch1);
        Switch switch2=(Switch)findViewById(R.id.switch2);
        final SharedPreferences sp=getSharedPreferences("userInfo", MODE_PRIVATE);
        boolean choose=sp.getBoolean("choose",false);
        boolean choose2=sp.getBoolean("choose2",false);

        switch1.setChecked(choose);
        switch2.setChecked(choose2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值