SharedPreferences(保存偏好参数)
来源:互联网 发布:js 声明 字符串数组 编辑:程序博客网 时间:2024/05/16 00:42
初次接触SharedPreferences,借助code-pig的博客写的小demo。实现每次打开app自动填充上次推出时输入的名字和年龄。
工具类SharedService
package com.example.sharedpreference;import java.util.HashMap;import java.util.Map;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.widget.Toast;public class SharedService { private Context context ; public SharedService(Context context) { // TODO Auto-generated constructor stub this.context = context ; } public void save(String name,String age) { SharedPreferences sp = context.getSharedPreferences("base", Context.MODE_WORLD_READABLE) ; Editor edit = sp.edit() ; edit.putString("name", name) ; edit.putString("age", age) ; edit.commit() ; Toast.makeText(context, "信息已经成功写入", Toast.LENGTH_LONG) ; } public Map<String,String> read() { Map<String,String> map = new HashMap<String, String>() ; SharedPreferences sp = context.getSharedPreferences("base", Context.MODE_WORLD_READABLE) ; map.put("name", sp.getString("name", "")) ; map.put("age", sp.getString("age", "")) ; return map ; }}
主activity
package com.example.sharedpreference;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener; import android.widget.Button;import android.widget.EditText;public class MainActivity extends ActionBarActivity {private EditText name ;private EditText age ;private Button ok ;private SharedService ss ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);this.name = (EditText) findViewById(R.id.name) ;this.age = (EditText)findViewById(R.id.age) ;this.ok = (Button) findViewById(R.id.ok) ;ss = new SharedService(this) ;this.name.setText(ss.read().get("name"));this.age.setText(ss.read().get("age"));this.ok.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubss.save(name.getText().toString(), age.getText().toString());}});}}
布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.sharedpreference.MainActivity" > <EditText android:id="@+id/name" android:layout_width="fill_parent" android:hint="输入姓名" android:layout_height="wrap_content"/> <EditText android:id="@+id/age" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="输入年龄"/> <Button android:id="@+id/ok" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="确定"/></LinearLayout>
0 0
- SharedPreferences(保存偏好参数)
- 采用SharedPreferences偏好参数保存
- 使用SharedPreferences保存偏好参数
- SharedPreferences(保存用户偏好参数)保存数据
- SharedPreferences(保存用户偏好参数)
- 采用SharedPreferences保存用户偏好设置参数
- 采用SharedPreferences保存用户偏好设置参数
- Android采用SharedPreferences保存用户偏好设置参数
- Android数据存储方式(二)SharedPreferences(偏好参数保存)
- 实例教程五:采用SharedPreferences保存用户偏好设置参数
- Android采用SharedPreferences保存用户偏好设置参数
- Android数据存储方式(二)SharedPreferences(偏好参数保存)
- 16_采用SharedPreferences保存用户偏好设置参数
- 数据存储与访问之SharedPreferences(偏好参数保存)
- 数据类操作之SharedPreferences(保存用户偏好参数)
- 数据类操作之SharedPreferences(保存用户偏好参数)
- Android数据存储方式(二)SharedPreferences(偏好参数保存)
- Android偏好参数SharedPreferences
- 第一章作业
- HDU3308
- git版本管理工具
- Java 正确使用 Volatile 变量
- JavaScript DOM 学习(2) 获取属性结点
- SharedPreferences(保存偏好参数)
- sql server left join 变量或常量,与oracle 加号+类似的
- 【RFID第二次作业】课后习题第6题
- Lisa随手记 1.8开发日志
- Django学习/问题汇总
- 杭电 HDU 1098 Ignatius's puzzle
- 10313 - Pay the Price
- 阻塞机制下的recv小结
- 操作系统第三次作业