您的位置:首页 > 移动开发 > Android开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息