27.02.2014 Views

Android 开发教程

Android 开发教程

Android 开发教程

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

-----------------------------------<strong>Android</strong> 编 程 基 础<br />

数 据 共 享 SharedPreferences<br />

1 数 据 请 求 端 , 线 先 开 启 另 一 个 Activity<br />

Intent startNextLayout=new<br />

Intent();<br />

startNextLayout.setClass(myEx.this<br />

this,choiceGroupActivity.class<br />

class);<br />

startActivity(startNextLayout);<br />

// 开 启 另 一 个 Activity<br />

2 数 据 选 择 端 , 设 置 好 数 据 , 利 用 实 现 了 SharePreferences 接 口 的 Editor 子 类 添 加 并 修 改 数 据<br />

Editor passfileEditor=getSharedPreferences("ITEM", 0).edit();<br />

3<br />

passfileEditor.putString("ChoiceKye", myContentString);<br />

// 设 置 数 据 Key, 并 添 加 数 据 myContentString 到 共 享 区<br />

数 据 选 择 端 , 存 入 数 据 , 关 闭 Activity。<br />

passfileEditor.commit();// 委 托 , 存 入 数 据<br />

finish();//<br />

获 得 实 现 了 SharePreferences<br />

接 口 的 Editor 对 象<br />

4<br />

数 据 请 求 端 , 利 用 SharePreferences 接 口 来 获 取 数 据 Key, 然 后 通 过 getString(String Key,String<br />

Defaultvalue) 来 获 得 Key 中 数 据<br />

SharedPreferences sharedPreferences=getSharedPreferences("ITEM", 0);<br />

// 得 到 共 享 区 "ITEM" 的 接 口 引 用<br />

String show=sharedPreferences.getString("ChoiceKye", "there is NONE")<br />

/* 利 用 Key"ChoiceKye" 从 SharePreferences 接 口 引 用 中<br />

得 到 ChoiceKye 的 内 容 , 没 有 时 为 默 认 值 "there is NONE"*/<br />

TextView showTextView=(TextView)findViewById(R.id.showR);<br />

showTextView.setText(show);// 显 示 ChoiceKye 的 内 容<br />

10

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!