Android中SharedPreferences存储详解(例子:记住密码)
2015-09-15 10:35
555 查看
1、废话不说了,先上代码
public class MainActivity extends ActionBarActivity implements OnClickListener{private EditText name;
private EditText pwd;
private CheckBox save;
private Button login;
private SharedPreferences preferences;
private Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
initEvent();
}
private void initData() {
// TODO Auto-generated method stub
preferences=getSharedPreferences("myifo", MODE_PRIVATE);
editor=preferences.edit();
}
private void initEvent() {
// TODO Auto-generated method stub
login.setOnClickListener(this);
}
private void initView() {
// TODO Auto-generated method stub
name=(EditText) findViewById(R.id.name);
pwd=(EditText) findViewById(R.id.pwd);
save=(CheckBox) findViewById(R.id.save_ifo);
login=(Button) findViewById(R.id.login);
//判断是否记住密码
if (preferences.getBoolean("checked", false)) {
save.setChecked(true);
name.setText(preferences.getString("name", null));
pwd.setText(preferences.getString("pwd", null));
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (TextUtils.isEmpty(name.getText())||TextUtils.isEmpty(pwd.getText())) {
Toast.makeText(getApplicationContext(), "用户名和密码不能为空!", Toast.LENGTH_SHORT).show();
}else if (save.isChecked()) {
//将信息存储自xml文件中
editor.putString("name",name.getText().toString());
editor.putString("pwd",pwd.getText().toString());
editor.putBoolean("checked", true);
editor.commit();
Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
}else {
editor.putBoolean("checked", false);
editor.commit();
Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
}
}
}
2、布局文件一起附上
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
<CheckBox
android:id="@+id/save_ifo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
</LinearLayout>
3、效果图
4、总结
SharedPreferences一般用于存储项目中一些配置信息,最常用的就是记住密码和自动登录,信息是以键值对的方式进行存取的,存储的信息一般也是比较简单的,存储的数据类型一般是string、boolbean、int、long等简单数据类型通过使用Editor来向文件写入信息,然后通过commit()提交写入的信息。
其他就不多说了,简单试用一下就ok
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories