实例教程五:采用SharedPreferences保存用户偏好设置参数

android水管工人游戏源码
http://www.eoeandroid.com/thread-207446-1-1.html

Android推箱子小游戏
http://www.eoeandroid.com/thread-207439-1-1.html

仿360恶意广告拦截扫描UI效果,有图有源码有真相
http://www.eoeandroid.com/thread-207522-1-1.html

<?xml version="1.0" encoding="utf-8"?>

 

<resources>

 

    <string name="hello">Hello World, MainActivity!</string>

    <string name="app_name">软件参数设置</string>

    <string name="name">姓名</string>

    <string name="age">年龄</string>

    <string name="save">保存参数</string>

    <string name="success">保存完成</string>

 

</resources>
package cn.itcast.service;

 

import java.util.HashMap;

import java.util.Map;

import android.content.Context; 

import android.content.SharedPreferences; 

import android.content.SharedPreferences.Editor;

 



public class PrefercesService {

 

        private Context context;        

        public PrefercesService(Context context){ 

                this.context = context; 

        }

 



        /** 

         * 保存参数 

         * @param name 姓名

         * @param age 年龄 

         */

 

        public void save(String name, Integer age) {

 

                SharedPreferences perferences = context.getSharedPreferences("itcast", Context.MODE_PRIVATE); 

                Editor editor = perferences.edit(); 

                editor.putString("name", name); 

                editor.putInt("age", age); 

                editor.commit();

 

        } 

        

        /** 

         * 获取各项配置参数 

         * @return 

         */

 

        public Map<String, String> getPreferces(){

 

                Map<String, String> params = new HashMap<String, String>();

                SharedPreferences perferences = context.getSharedPreferences("itcast", Context.MODE_PRIVATE);

                params.put("name", perferences.getString("name", null));

                params.put("age", String.valueOf(perferences.getInt("age", 0)));

                return params;

 

        }

 

}

 
package cn.itcast.settings;

 



import java.util.Map;

 



import cn.itcast.service.PrefercesService;

 

import android.app.Activity;

 

import android.os.Bundle;

 

import android.view.View;

 

import android.widget.EditText;

 

import android.widget.Toast;

 



public class MainActivity extends Activity {

 

    private EditText edtName;

 

    private EditText edtAge;

 

    private PrefercesService service;

 

    

    @Override

 

    public void onCreate(Bundle savedInstanceState) {

 

        super.onCreate(savedInstanceState);

 

        setContentView(R.layout.main);

 

        

 

        edtName = (EditText)this.findViewById(R.id.edtName);

 

        edtAge = (EditText)this.findViewById(R.id.edtAge);

 

        

 

        service = new PrefercesService(this);

 

        

 

        Map<String, String> params = service.getPreferces();

 

        edtName.setText(params.get("name"));

 

        edtAge.setText(params.get("age"));

 

    }

 

    

    public void save(View v){

 

            String name = edtName.getText().toString();

 

            String age = edtAge.getText().toString();

 

            

            

            service.save(name, Integer.valueOf(age));            

            Toast.makeText(getApplicationContext(), R.string.success, 1).show();

 

    }

 

}

 

你可能感兴趣的:(实例教程五:采用SharedPreferences保存用户偏好设置参数)