zoukankan      html  css  js  c++  java
  • 黎活明8天快速掌握android视频教程16_采用SharedPreferences保存用户偏好设置参数

    SharedPreferences保存的数据是xml格式,也是存在数据保存的下面四种权限:

    我们来看看

    我们来看看具体的业务操作类:

    /**
     * 文件名:SharedPrecences.java
     * 版权:版权所有 (C) 中国电科30所三部
     * 描述:
     * 修改人: wei.yuan
     * 修改时间:2015/1/8
     * 修改内容:新增
     */
    package service;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    
    import java.util.HashMap;
    import java.util.Hashtable;
    import java.util.Map;
    
    /**
     * 项目名称:SharedPreferences
     * 类描述:
     * 创建人:wei.yuan
     * 创建时间:2015/1/8 10:29
     * 修改人:wei.yuan
     * 修改时间:2015/1/8 10:29
     * 修改备注:
     * 版权:版权所有 (C) 中国电科30所三部
     */
    public class SharedPrecences {
        private Context context;
    
        public SharedPrecences(Context context) {
            this.context = context;
        }
        public void saveSharedPrecences(String name ,Integer age)
        {
            SharedPreferences sharedPrecences = context.getSharedPreferences("123", context.MODE_PRIVATE);//默认的格式是xml,123就不需要写后缀名
            SharedPreferences.Editor editor = sharedPrecences.edit();//编辑文字
            editor.putString("name",name);
            editor.putInt("age", age);
            editor.commit();//保存之后记得提交
    
    
        }
    /*
    public void save(String name , Integer age) throws Exception{
        SharedPreferences preference = context.getSharedPreferences("sclead", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preference.edit();
        editor.putString("name", name);
        editor.putInt("age", age);
        editor.commit();//把数据提交会文件
    }
    */
    
        public  Map<String ,String> show()
        {
            Map<String,String > map = new Hashtable<String, String>();
            SharedPreferences sharedPrecences = context.getSharedPreferences("123", context.MODE_PRIVATE);//默认的格式是xml,123就不需要写后缀名
            map.put("name",sharedPrecences.getString("name","查找的字段不存在"));
            map.put("age",String.valueOf(sharedPrecences.getInt("age", 0)));
    
    
    
            return  map;
        }
    /*
        */
    /**
         * 获取各项配置参数
         * @return
         *//*
    
        public Map<String, String> getPreferences(){
            Map<String, String> maps = new HashMap<String, String>();
            SharedPreferences preference = context.getSharedPreferences("sclead", Context.MODE_PRIVATE);
            maps.put("name", preference.getString("name", "你查找的字段不存在"));
            maps.put("age", String.valueOf(preference.getInt("age", 0)));
            return maps;
        }
    */
    
    }

    我们来看看activity的代码是:

    package test.weiyuan.sharedpreferences;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import java.util.Map;
    
    import service.SharedPrecences;
    
    
    public class MyActivity extends Activity {
        private EditText name1,age1;
        private TextView showText;
        private Button saveButton ,showButton;
    
        SharedPrecences sharedPrecences = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);
            name1 = (EditText)this.findViewById(R.id.name);
            age1 = (EditText)this.findViewById(R.id.age);
            saveButton = (Button)this.findViewById(R.id.savebutton);
            showButton = (Button)this.findViewById(R.id.showButton);
            showText = (TextView)this.findViewById(R.id.showText);
    
             sharedPrecences = new SharedPrecences(getApplicationContext());
    
    
    
             saveButton.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
    
                     try {
                        /* sharedPrecences.save(name1.getText().toString(), Integer.valueOf(age1.getText().toString()));*/
                         String str = name1.getText().toString();
                         String str1 = age1.getText().toString();
                         sharedPrecences.saveSharedPrecences(str, Integer.valueOf(str1));
                         Toast.makeText(getApplicationContext(), "数据保存成功", Toast.LENGTH_LONG).show();
                     } catch (Exception e) {
                         e.printStackTrace();
                     }
    
                 }
             });
           showButton.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
            Map<String,String> map = sharedPrecences.show();
            name1.setText(map.get("name"));
                   Log.i("wy",map.get("name"));
                   showText.setText(map.get("name")+map.get("age"));
               }
    
           });
        }
    
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.my, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }
  • 相关阅读:
    彻底弄懂flex布局
    剖析Vue原理&实现双向绑定MVVM
    【Mysql进阶-3】大量实例悟透EXPLAIN与慢查询
    mysql 排序
    从ReentrantLock的实现看AQS的原理及应用
    Java面试之Synchronized解析
    基于vue-cli搭建vue项目开发环境
    在win环境下使用yarn安装 vue-cli
    优化器追踪示例
    MySQL常见的七种锁详细介绍
  • 原文地址:https://www.cnblogs.com/kebibuluan/p/6757460.html
Copyright © 2011-2022 走看看