黎活明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 2017-04-24 16:21 luzhouxiaoshuai 阅读(153) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!