Android中SharedPreferences存储详解(例子:记住密码)

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值