android四种数据存储方式,Android——数据存储(四种方式之一)SharedPrefereces
1.SharedPrefereces 轻量级.XML 存储文件名,数据保存在data/data/basepackage/shared_prefs/myopt.xml中 实例-收藏-记住密码自动登录//一种轻量级的数据存储方式//通过KEY存入数据——putxxxx(key,value)取出数据——getxxxx(key default)2.读写SD卡 SD的根目录 适用于数据流读写3.SQLite
1.SharedPrefereces 轻量级.XML 存储文件名,数据保存在data/data/basepackage/shared_prefs/myopt.xml中 实例-收藏-记住密码自动登录
//一种轻量级的数据存储方式//通过KEY
存入数据——putxxxx(key,value)
取出数据——getxxxx(key default)
2.读写SD卡 SD的根目录 适用于数据流读写
3.SQLite 轻量级.dp文件多用于手机里
4.Content prowvider内容提供者
网路存储 在网络后台存储
package com.example.jreduch08;
import android.content.Intent;
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;
public class SharePreferencesActivity extends AppCompatActivity {
private CheckBox cb1;
private CheckBox cb2;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1= (CheckBox) findViewById(R.id.checkBox1);
cb2= (CheckBox) findViewById(R.id.checkBox2);
Button save2= (Button) findViewById(R.id.save2);
final EditText user= (EditText) findViewById(R.id.user);
final EditText pwd= (EditText) findViewById(R.id.pwd);
Button save= (Button) findViewById(R.id.save);
sp=getSharedPreferences("userInfo",MODE_PRIVATE);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor=sp.edit();
editor.putString("userName",user.getText().toString());
editor.putInt("userPwd",Integer.parseInt(pwd.getText().toString()));
editor.commit();
Intent intent=new Intent(SharePreferencesActivity.this,Sp2Activity.class);
startActivity(intent);
}
});
save2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor=sp.edit();
// editor.putString("userName",user.getText().toString());
// editor.putInt("userPwd",Integer.parseInt(pwd.getText().toString()));
editor.putBoolean("jzmm",cb1.isChecked());
editor.putBoolean("zddl",cb2.isChecked());
editor.commit();
Intent intent=new Intent(SharePreferencesActivity.this,SettingsActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
sp=getSharedPreferences("userInfo",MODE_PRIVATE);
Boolean jzmm=sp.getBoolean("jzmm",false);
Boolean zddl=sp.getBoolean("zddl",false);
cb1.setChecked(jzmm);
cb2.setChecked(zddl);
}
}
package com.example.jreduch08;
import android.content.Intent;
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.Switch;
public class SettingsActivity extends AppCompatActivity {
private Switch switch1;
private Switch switch2;
private SharedPreferences sp;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
switch1= (Switch) findViewById(R.id. switch1);
switch2= (Switch) findViewById(R.id. switch2);
button= (Button) findViewById(R.id.button);
sp=getSharedPreferences("userInfo",MODE_PRIVATE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor=sp.edit();
editor.putBoolean("jzmm",switch1.isChecked());
editor.putBoolean("zddl",switch2.isChecked());
editor.commit();
Intent intent=new Intent(SettingsActivity.this,SharePreferencesActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
sp=getSharedPreferences("userInfo",MODE_PRIVATE);
Boolean jzmm=sp.getBoolean("jzmm",false);
Boolean zddl=sp.getBoolean("zddl",false);
switch1.setChecked(jzmm);
switch2.setChecked(zddl);
}
}
package com.example.jreduch08;
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.TextView;
public class Sp2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sp2);
Button getUser= (Button) findViewById(R.id.getUser);
final TextView showUser= (TextView) findViewById(R.id.showUser);
final SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
getUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name=sp.getString("userName","Admin");
String pwd=String.valueOf(sp.getInt("userPwd",88888));
showUser.setText("用户名"+name+"n"+"密码"+pwd);
}
});
}
}
//图片轮播的控件
//图片自动缩放的控件
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.jude:rollviewpager:1.3.4'
compile 'com.flaviofaria:kenburnsview:1.0.7'
compile 'com.android.support:design:24.0.0'
package com.example.jreduch08;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.flaviofaria.kenburnsview.KenBurnsView;
import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;
public class ZyfActivity extends AppCompatActivity {
private RollPagerView rollPagerView;
private KenBurnsView kenBurnsView;
//图片数组
private int[]imgs={
R.mipmap.a,
R.mipmap.b,
R.mipmap.c,
R.mipmap.d,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zyf);
rollPagerView= (RollPagerView) findViewById(R.id.roll_view_pager);
kenBurnsView= (KenBurnsView) findViewById(R.id.image);
//设置播放时间间隔
rollPagerView.setPlayDelay(3000);
//设置透明度
rollPagerView.setAnimationDurtion(500);
//设置适配器
rollPagerView.setAdapter(new TestNormalAdapter(imgs));
//自定义指示器图片
// rollPagerView.setHintView(new IconHintView(this,R.mipmap.e,R.mipmap.d));
//设置圆点指示器颜色
rollPagerView.setHintView(new ColorPointHintView(this, Color.YELLOW,Color.WHITE));
//文字指示器
// rollPagerView.setHintView(new TextHintView(this));
//隐藏指示器
// rollPagerView.setHintView(null);
rollPagerView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(int position) {
kenBurnsView.setImageResource(imgs[position]);
}
});
}
private class TestNormalAdapter extends StaticPagerAdapter{
private int []imgs;
public TestNormalAdapter(int[] imgs){
this.imgs=imgs;
}
@Override
public View getView(ViewGroup container, int position) {
ImageView view=new ImageView(container.getContext());
view.setImageResource(imgs[position]);
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
return view;
}
@Override
public int getCount() {
return imgs.length;
}
}
}
//加载网络图片
compile 'com.github.bumptech.glide:glide:3.7.0'
package com.example.jreduch08;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
public class GlideActivity extends AppCompatActivity {
private ImageView img;
private Button bt1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glide);
img = (ImageView) findViewById(R.id.img);
bt1 = (Button) findViewById(R.id.bt1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Glide.with(GlideActivity.this)
.load("https://img02.sogoucdn.com/net/a/04/link?url=http%3A%2F%2Fi04" +
".pictn.sogoucdn.com%2F59700dc8568d7ff3&appid=122")
.asGif()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.error(R.mipmap.ic_launcher)
.into(img);
}
});
}
}
更多推荐
所有评论(0)