三大存储方式之一:SharedPreferences 存储

SharedPreferences存储的最大特点之一是采取键值对的方式来存储数据,当从文件中读取数字的时候可以很方便的根据键来获取值,我们经常在浏览器,QQ等应用中使用的记住密码功能就是采用该类存储方式实现的,以下是登录界面中常用的记住密码功能的代码示例:

1.先建立一个文件名为activity_main.xml的登录界面的布局文件:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:stretchColumns="1">    //当布局宽度没有被完全填充时,指定某一列自由拉伸,使得宽度被填充完全,该处指定第一列(从第0列开始)自由拉伸
<TableRow>
    <TextView
        android:layout_height="wrap_content"
        android:text="Account"
        />
    <EditText
        android:id="@+id/account"
        android:layout_height="wrap_content"
        android:hint="Input your account"
        />
</TableRow>
<TableRow>
    <TextView
        android:layout_height="wrap_content"
        android:text="Password"
        />
    <EditText
        android:id="@+id/password"
        android:layout_height="wrap_content"
        android:inputType="textPassword"

        />
</TableRow>
<TableRow>
    <CheckBox 
        android:id="@+id/remember_pass"
        android:layout_height="wrap_content"
        />
    <TextView 
        android:layout_height="wrap_content"
        android:text="Remember password"
        />
</TableRow>
<TableRow>
    <Button 
        android:id="@+id/login"
        android:layout_height="wrap_content"
        android:layout_span = "2"//表示两列自动合并为一列显示
        android:text="Login"
        />
    </TableRow>    
</TableLayout>


2.建立MainActivity.java文件,实现主功能

package com.example.mysharedpreferences;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {
EditText accoutEditText;
EditText passwordEditText;
CheckBox rememberpassword;
Button loginButton;
SharedPreferences preferences;
SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        accoutEditText = (EditText)findViewById(R.id.account);
        passwordEditText = (EditText)findViewById(R.id.password);
        rememberpassword = (CheckBox)findViewById(R.id.remember_pass);
        loginButton = (Button)findViewById(R.id.login);
        //获取preferences对象,通过以下方式自动将应用的包名来命名sharedpreferences存储文件
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        //判断上一次登录是否勾选了记住密码的checkbox,是否被勾选的值存储在上面建立的文件夹中,通过上次登录的时候存储
        if(preferences.getBoolean("rememberpassword", false)){
        //如果勾选了记住密码,则从存储文件中获取上次登录的账号和密码,并写在编辑框中,而且默认将勾选框选中
        accoutEditText.setText(preferences.getString("account", ""));
        passwordEditText.setText(preferences.getString("password", ""));
        rememberpassword.setChecked(true);
        }
        //点击登录按钮,从账号和密码编辑框重新获得账号密码,如果正确登录成功跳转页面,如果错误出现提示
        loginButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String account = accoutEditText.getText().toString();
String password = passwordEditText.getText().toString();
if(account.equals("admin")&&password.equals("123456")){
//获得可对preferences文件进行写操作的对象editor
editor = preferences.edit();
//如果记住密码勾选框选中,则将账号密码写入sharedpreferences对象文件中
//并且将rememberpassword的值设为ture存储,下次登录获取该值判断为ture后则可自动获取输入账号密码
if(rememberpassword.isChecked()){
editor.putString("account", account);
editor.putString("password", password);
editor.putBoolean("rememberpassword", true);

}else{
//如果记住密码勾选框没有被选中,不往存储文件存数据
editor.clear();
}
//向文件夹提交数据
editor.commit();
//提交成功后通过intent跳转到主页
Intent intent = new Intent(MainActivity.this,AnotherActivity.class);
startActivity(intent);
finish();

}else {
Toast.makeText(MainActivity.this, "account or password is invalid", Toast.LENGTH_LONG).show();
}
}
});
    }
}


3.补充下登录成功后被跳转的界面布局(activity_another.xml)以及活动(AnotherActivity.java)

activity_another.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mysharedpreferences.AnotherActivity" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />


</RelativeLayout>


AnotherActivity.java:

package com.example.mysharedpreferences;


import android.app.Activity;
import android.os.Bundle;


public class AnotherActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值