黎活明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);
    }
}
复制代码

 

posted on   luzhouxiaoshuai  阅读(153)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示