码迷,mamicode.com
首页 > 移动开发 > 详细

Android——数据存储(四种方式之一)SharedPrefereces

时间:2016-08-12 23:58:46      阅读:800      评论:0      收藏:0      [点我收藏+]

标签:


Android——数据存储(四种方式)

1.SharedPrefereces   轻量级.XML  存储文件名,数据保存在data/data/basepackage/shared_prefs/myopt.xml中   实例-收藏-记住密码自动登录

//一种轻量级的数据存储方式//通过KEY

存入数据——putxxxxkeyvalue

取出数据——getxxxxkey  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);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"

    tools:context=".SharePreferencesActivity">
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入用户名"
    android:id="@+id/user"
    />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:id="@+id/pwd"
        android:layout_below="@+id/user"
        android:layout_alignParentStart="true" />
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/save"
    android:text="保存"
    android:layout_below="@+id/checkBox2"
    android:layout_alignParentStart="true" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自动登录"
        android:id="@+id/checkBox2"

        android:layout_alignTop="@+id/checkBox1"
        android:layout_centerHorizontal="true"
        android:checked="false" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:id="@+id/checkBox1"
        android:layout_below="@+id/pwd"
        android:layout_alignParentStart="true"
        android:checked="false" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存2"
        android:id="@+id/save2"
        android:layout_below="@+id/save"
        android:layout_alignParentStart="true" />

</RelativeLayout>

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);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"

    tools:context="com.example.jreduch08.SettingsActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"

    >



</LinearLayout>

    <Switch
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="记住密码"
        android:id="@+id/switch1"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:checked="false" />

    <Switch
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="自动登录"
        android:id="@+id/switch2"
        android:layout_below="@+id/switch1"
        android:layout_alignParentStart="true"
        android:checked="false" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存"
        android:id="@+id/button"
        android:layout_below="@+id/switch2"
        android:layout_alignParentStart="true" />
</RelativeLayout>

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);
            }
        });

    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.example.jreduch08.Sp2Activity">
<TextView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/showUser"
    />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/getUser"
        android:text="获取用户数据"
        android:layout_alignBottom="@+id/showUser"
        android:layout_alignParentStart="true" />
</RelativeLayout>

技术分享

技术分享

技术分享

技术分享



 //图片轮播的控件
    //图片自动缩放的控件
    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;
        }
    }

}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"

    tools:context="com.example.jreduch08.ZyfActivity">
<com.jude.rollviewpager.RollPagerView
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:id="@+id/roll_view_pager"
    app:rollviewpager_play_delay="5000"
    ></com.jude.rollviewpager.RollPagerView>
    <com.flaviofaria.kenburnsview.KenBurnsView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/roll_view_pager"/>

</RelativeLayout>
技术分享


  //加载网络图片
    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);
            }
        });
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"

    tools:context="com.example.jreduch08.GlideActivity">
<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:id="@+id/img"
    />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt1"
        android:text="加载图片"
        android:layout_below="@+id/img"
        />
</RelativeLayout>

技术分享


Android——数据存储(四种方式之一)SharedPrefereces

标签:

原文地址:http://blog.csdn.net/zhangyufeng0126/article/details/52194719

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!