SharePreference存储技术在android中主要应用于保存一些简单信息,提高用户体验性,通常用于保存用户登录信息中,下面是一个使用SharePreference存储的小示例。代码如下:
首先是部局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="姓名" android:textSize="20dp" /> <EditText android:id="@+id/etName" android:layout_width="150dp" android:layout_height="wrap_content" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="爱好" android:textSize="20dp" /> <EditText android:id="@+id/etHabit" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <CheckBox android:id="@+id/cbEmployee " android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="是否工作" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="单位性质" android:textSize="20dp" /> <RadioGroup android:id="@+id/rgCompanyType" android:layout_width="fill_parent" android:layout_height="wrap_content"> <RadioButton android:id="@+id/rbCompany1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="国营" /> <RadioButton android:id="@+id/rbCompany2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="私营" /> <RadioButton android:id="@+id/rbCompany3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="股份制" /> </RadioGroup> </LinearLayout>
主程序代码:
import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.CompoundButton.OnCheckedChangeListener; public class Main extends Activity implements OnCheckedChangeListener { //定义存在文件名称 private final String PREFERENCE_NAME = "survey"; private EditText name; private EditText habit; private CheckBox cbEmployee; private RadioGroup rgCompanyType; private RadioButton rbCompany1; private RadioButton rbCompany2; private RadioButton rbCompany3; @Override protected void onStop() { /** * 关闭程序调用,保存信息 */ SharedPreferences mySharedPreferences = getSharedPreferences( PREFERENCE_NAME, Activity.MODE_PRIVATE); SharedPreferences.Editor editor = mySharedPreferences.edit(); //获取要保持信息,进行存储 editor.putString("name", name.getText().toString()); editor.putString("habit", habit.getText().toString()); editor.putBoolean("employee", cbEmployee.isChecked()); editor.putInt("companyTypeId", rgCompanyType.getCheckedRadioButtonId()); //提交事务 editor.commit(); super.onStop(); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { rbCompany1.setEnabled(isChecked); rbCompany2.setEnabled(isChecked); rbCompany3.setEnabled(isChecked); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); name = (EditText) findViewById(R.id.etName); habit = (EditText) findViewById(R.id.etHabit); cbEmployee = (CheckBox) findViewById(R.id.cbEmployee); rgCompanyType = (RadioGroup) findViewById(R.id.rgCompanyType); rbCompany1 = (RadioButton) findViewById(R.id.rbCompany1); rbCompany2 = (RadioButton) findViewById(R.id.rbCompany2); rbCompany3 = (RadioButton) findViewById(R.id.rbCompany3); //为单选按钮设置监听 cbEmployee.setOnCheckedChangeListener(this); /** * 获取SharedPreferences对象用于存储操作 * @parma name 文件名称 * @parma mode */ SharedPreferences sharedPreferences = getSharedPreferences( PREFERENCE_NAME, Activity.MODE_PRIVATE); /** * 设置初始默认值 * sharedPreferences调用get()方法获取对象中的值 * 第一个参数为取值的key,第二个参数为默认值 */ name.setText(sharedPreferences.getString("name", "")); habit.setText(sharedPreferences.getString("habit", "")); cbEmployee.setChecked(sharedPreferences.getBoolean("employee", false)); rgCompanyType.check(sharedPreferences.getInt("companyTypeId", -1)); onCheckedChanged(cbEmployee, cbEmployee.isChecked()); } }
发表评论
-
android中退出整个app应用程序
2012-07-19 13:45 1809可以通过一个列表来维护所有的activity,在需要退出的时候 ... -
有关Activity的Launch mode 以及Intent的setFlags(转载)
2012-05-15 15:02 0Activity有四种加载模式 ... -
android 开发的性能原则
2012-02-29 09:51 0手机的开发由于受到性能,电池等硬件的瓶颈,所以在开发应用的时候 ... -
android开发的9个原则
2011-11-15 14:43 9511:如果要使用全局文件,可以把变量放入Applacat ... -
Android开发之编程中15个很有用的代码片段
2011-11-15 14:23 9451:查看是否有存储卡插入 String status ... -
keytool生成数字证书
2011-11-01 14:03 1076JDK中keytool常用命令-genkey 在用户主目录中创 ... -
android中的http访问方式
2011-09-27 16:40 1591Android网络编程之Http通信 原创作品,允许 ... -
android开发综合应用之打分应用
2011-09-21 16:44 1631该程序是一个listView跟ratingbar综合的应用示例 ... -
android 开发之ratingbar
2011-09-21 16:25 2117RatingBar组件是一个打分组件,主要用于对应用打分,下面 ... -
android开发之Spinner组件
2011-09-21 08:54 2551Spinner组件组要用显示一个下拉列表,在使用中需要用到适配 ... -
eclipse中查看android的SDK源代码
2011-09-15 15:32 831原理: http://log4think.com/brow ... -
android开发之contentprovider(转)
2011-09-14 17:27 946ContentProvider的作用: 1、为存储和获取数据 ... -
设置android模拟器上网(转)
2011-08-31 11:10 1102很多网友也问到为啥自己在家的PC机可以上网,而运行在PC机上面 ... -
android基础开发之二intent(意图)用法
2011-08-29 15:27 2275android中不同的activity之间的切换主要是通过in ... -
android基础开发之一setContentView用法
2011-08-29 15:05 14656android开发中如果想实现布局页面的跳转可以使用setCo ... -
android连接真实手机
2011-08-29 10:09 1227用实现用真机调试你的程序,整个操作相当的方便简单 1、首先用 ... -
android中各种permissiond详解(转)
2011-08-29 10:04 1092Android应用程序在使用很多功能的时候必须在Mainife ...
相关推荐
Android游戏保存之SharedPreference使用Demo源码.rar
Android数据存储技术中的SharedPreference存储,包括读取,删除,读取其他程序的SharedPreference
Android应用开发
android SharedPreference实现登录.开发工具是:e 本人博客是:day_moon
SharedPreference跨应用读写,适用于自己公司的多个APP,具体见blog: http://blog.csdn.net/hwe_xc/article/details/50788591 注意:两个应用签名必须一样。
android studio实现文件存储和sharedPreference存储,以QQ登陆为例,将登录的用户名和密码保存到后台,有2种方式
Android之SharedPreference
android Sharedpreference是共享存储,本例子代码实现存储数据,提取数据。代码导入就可以运行。
This Android library let's you edit your application SharedPreferences from the device itself. Screenshot Usage It's a one-liner: // From a button click in your debug menu or something Preferator....
一个比较安全的SharedPreference加密方案
Android应用源码之(游戏保存之SharedPreference).zip项目安卓应用源码下载Android应用源码之(游戏保存之SharedPreference).zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司...
android开发--用SharedPreference读写
android sharedpreference 简易实现,学习android自己练习用的
Android四种存储方式 sharedpreference,file,SQlite,contentprovider 实现创建数据库和表及增删改查
源码参考,欢迎下载
安卓Android源码——(游戏保存之SharedPreference).zip
适用于Android的Easy SharedPreference引擎
android新手之用户配置(SharedPreference)存储 点击第一个Button时,把EditText中输入的内容写入到用户配置中 点击第二个Button时,读出用户配置中的内容显示到TextView中
根据本讲学习内容,学会运用SharedPreference存储数据,读取数据。 (1)创建一个简单的登录界面。 (2) 提交的作业包中含有应用程序运行效果截图。