SP存储和读取

  

尚硅谷demo

package com.atguigu.l04_datastorage;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

/**
* 测试sp存储的界面
*/
public class SpActivity extends Activity {

private EditText et_sp_key;
private EditText et_sp_value;

private SharedPreferences sp;

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

et_sp_key = (EditText) findViewById(R.id.et_sp_key);
et_sp_value = (EditText) findViewById(R.id.et_sp_value);

//1. 得到sp对象
sp = getSharedPreferences("atguigu", Context.MODE_PRIVATE);
}

public void save(View v) {
//2. 得到editor对象
Editor edit = sp.edit();
//3. 得到输入的key/value
String key = et_sp_key.getText().toString();
String value = et_sp_value.getText().toString();
//4. 使用editor保存key-value
edit.putString(key, value).commit();
//5. 提示
Toast.makeText(this, "保存完成!", 0).show();
}

public void read(View v) {
//1. 得到输入的key
String key = et_sp_key.getText().toString();
//2. 根据key读取对应的value
String value = sp.getString(key, null);
//3. 显示
if(value==null) {
Toast.makeText(this, "没有找到对应的value", 0).show();
} else {
et_sp_value.setText(value);
}
}
}

第一行代码demo

package com.example.hzq_study;

import android.R.integer;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中
* @author 侯志强
*
*/
public class SPActivity extends Activity {

private Button savedata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
savedata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});
}
}package com.example.hzq_study;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 将数据存储到SharedPreferences中和从SharedPreferences中读取数据
* @author 侯志强
*
*/
public class HZQSPActivity extends Activity {

private Button savedata;
private Button restoredata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp);

savedata = (Button) findViewById(R.id.save_data);
restoredata = (Button) findViewById(R.id.restore_data);
savedata.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
}
});

restoredata.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
String name = pref.getString("name", "");
int age = pref.getInt("age", 0);
boolean married = pref.getBoolean("married", false);

}
});
}
}

时间: 2024-11-08 11:59:28

SP存储和读取的相关文章

数据存储——SP存储

一.存储方式分类:SharedPreferences存储 二.SharedPreferences存储 1.特点 ①存储单一数据,例如数值,字符串,布尔 ②文件:/date/date/包名/shared_prefs/xxx.xml:    <map><string  name="key">value</string></map> ③以键值对的形式存储 ④可以设置不被其他应用操作 2.API (1)SharedPreferences ①获取实

使用 SharedPreferences 实现数据的存储和读取

在开发的过程中我们必须遇到的就是如何对用户的数据进行有效的存储以及读取.我们举个例子,现在我们使用app,当我们登陆一个账号的时候选择记住密码软件就会记住我们的账号以及密码,我们退出当前账号,就可以直接点击登陆进入账号内部,而不需要再输入账号和密码了.那么这就是今天我们要说的,如何对用户输入的账号以及密码进行存储,并且进行显示.这里我们使用到了google工程师向我们推荐的一个API——SharedPreferences. 关于SharedPreferences 它是android.conten

[转] C#实现在Sql Server中存储和读取Word文件

出处 C#实现在Sql Server中存储和读取Word文件 要实现在Sql Server中实现将文件读写Word文件,需要在要存取的表中添加Image类型的列,示例表结构为: CREATE TABLE CONTRACTS ( ID VARCHAR (50), CONTRACT_FILE IMAGE ); 要将Word文件存储到数据库的CONTRACT_FILE字段中,需要将文件转换为byte数组,具体代码如下: /// 将文件转换为byte数组 /// <summary> /// 将文件转换

01- - -1.获得项目中info.plist文件的内容 2.沙盒的数据存储及读取 3.控制器view的高度和状态栏statusBar的关系 4.[UIScreen mainScreen].applicationFrame的取值 5.按钮的状态 6.错误调试技巧 7.按钮的各种状态设置

1.获得项目中info.plist文件的内容 1> [NSBundle mainBundle].infoDictionary 2> 版本号在info.plist中的key:kCFBundleVersionKey 2.沙盒的数据存储及读取 1> 数据存储: [[NSUserDefaults standardUserDefaults] setObject:version forKey:versionKey]; 存储数据时记得同步一下 [[NSUserDefaults standardUser

编译器架构的王者LLVM——(10)变量的存储与读取

LLVM平台,短短几年间,改变了众多编程语言的走向,也催生了一大批具有特色的编程语言的出现,不愧为编译器架构的王者,也荣获2012年ACM软件系统奖 -- 题记 版权声明:本文为 西风逍遥游 原创文章,转载请注明出处 西风世界 http://blog.csdn.net/xfxyy_sxfancy 变量的存储与读取 变量是一款编程语言中的核心,说编译语言是一种符号处理工具,其实是有些道理的.栈式符号表可以方便的记录编译过程中的变量和语法符号,我们上节已经了解了其中的实现方法.那么,还有没有其他的办

C/C++ 图像二进制存储与读取

本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50782792 在深度学习时,制作样本数据集时,须要产生和读取一些二进制图像的数据集,如MNIST,CIFAR-10等都提供了适合C语言的二进制版本号. 以CIFAR-10的数据集为例.官网上有两段关键的介绍: 二进制版本号数据集格式为(图像大小为32x32): <1 x label><3072 x pixel> ... &

blob的存储与读取

对于oracle数据库的blob的存储与读取对应的是byte数组. 将blob类型数据存入数据库: String blob: byte[] byte = blob.getBytes(); entity.setDoc(byte); 然后直接作为blob类型存入即可: 将blob类型数据取出在前台展示: String blobStr = String(entity.getBlob(),"UTF-8"); 然后就可以在前台展示了

iOS NSMutableDictionary中UIImage的存储和读取

思路:将UIImage转换成NSData,然后插入到NSMutableDictionary中,读取时,用NSData读出来,然后再转换成UIImage -存储 UIImage *image = [self getPicture: id]; NSData *imageData = UIImagePNGRepresentation(image); [tmpDic setObject:imageData forKey:@"imageData"]; -读取 NSData *imageData

基于ini配置文件实现多维数组数据的按行存储和读取

需求是为一个多维数组对象的数据按行存储到文件,需要键值对区分层级,对每个对象描述清晰.类似的格式如下: 上图中的数据对应的就是如下的数组(php 语言): $arr = array( '10003' => array( 'id' => 10003, 'tokentime' => 400), '10005' => array( 'id' => 10005, <p> 'cookie' => array(</p> 'num' => 20 ), '