• 实例教程五:采用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();
     
        }
     
    }
  • 相关阅读:
    mysql 查找数组格式的字符串中是否包含某个值
    假期总结
    shell循环结构解析:for/while/case
    ansible笔记(15):循环(二)with_items/with_list/with_together/with_flattened
    ansible笔记(14):循环(一)
    解决报错Failed to start LSB: Bring up/down networking:MAC地址导致
    实现ENSP模拟器与物理主机、虚拟机通信
    zabbix4.2配置监控华为路由器:基于ENSP模拟器
    Grafana展示zabbix监控数据
    zabbix4.2配置监控TCP连接状态
  • 原文地址:https://www.cnblogs.com/vus520/p/2729090.html
Copyright © 2020-2023  润新知