Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android——數據存儲(四種方式之一)SharedPrefereces

Android——數據存儲(四種方式之一)SharedPrefereces

編輯:關於Android編程

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




\

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved