您的位置:首页 > Web前端

使用SharedPreferences保存数据

2016-01-09 17:05 204 查看

SharedPreferences 保存的数据

1.说明一些关于SharedPreferences的知识

使用SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<packagename>/shared_prefs目录下。例如:



SharedPreferences sharedPreferences = getSharedPreferences("PREFERENCES_NAME",MODE_PRIVATE);

//edit()可以让对象处于编辑状态,用putXX保存值,并用commit提交,类似于数据库中的事物提交

sharedPreferences.edit().putString("userName",uName).putString("password",password).commit();

生产的xml文件.用浏览器打开



不多说了写代码吧!不懂的地方可以看看代码里面的注释

2.先看看xml里面的布局,说明一下勾选了记住密码后才能保存密码,默认勾选

在java代码里面会给判断的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!--文字建议写在String.xml文件里面,我这里是为了简单明了-->
    <EditText
        android:id="@+id/uNameEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"/>
    <EditText
        android:id="@+id/passwordEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <CheckBox
            android:id="@+id/userChecked"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码"/>
        <Button
            android:id="@+id/submitBtn"
            android:layout_marginLeft="150dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登陆"/>
    </LinearLayout>

</LinearLayout>


3下面就是java代码了

package com.smart.gaomin.sharedpreferences;
<pre class="java" name="code">import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;



public class MainActivity extends Activity {
    //定义SharedPreferences名称
    private static final String PREFERENCES_NAME ="userInfo";
    //定义控件变量
    private EditText userNameEText;
    private EditText passWordEText;
    private Button submitBtn;
    private CheckBox userChecked;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件
        userNameEText = (EditText) findViewById(R.id.uNameEditText);
        passWordEText = (EditText) findViewById(R.id.passwordEditText);
        submitBtn = (Button) findViewById(R.id.submitBtn);
        userChecked = (CheckBox) findViewById(R.id.userChecked);

        //获取xml文件里面保存的数据,后面那个参数,表示私有模式,有三种模式,
        // 自己用工具会自动生成,是关于外部应用访问本应用的权限,这里不多说
        SharedPreferences sharedPreferences = getSharedPreferences("PREFERENCES_NAME",MODE_PRIVATE);
        //后面那个参数是在没有取到值的情况下的默认值,前面都懂类似Map,其实保存在xml里面就是个Map
        String username = sharedPreferences.getString("userName", "");
        String password = sharedPreferences.getString("password","");

        //打开程序直接赋值给控件
        userNameEText.setText(username);
        passWordEText.setText(password);
        //点击事件实现跳转
        submitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String uName = userNameEText.getText().toString().trim();
                String password = passWordEText.getText().toString().trim();
                //通过TextUtil.isEmpty(editText.getText())   true表示是空,false表示非空
                if (TextUtils.isEmpty(uName) && TextUtils.isEmpty(password)) {
                    Toast.makeText(getApplicationContext(), "账号和密码不能为空", Toast.LENGTH_LONG).show();
                } else if(uName.equals("gaomin")&&password.equals("123456")){
                    if(userChecked.isChecked()){
                        Toast.makeText(getApplicationContext(), "登陆成功并保存密码", Toast.LENGTH_LONG).show();
                        //保存密码,
                        SharedPreferences sharedPreferences = getSharedPreferences("PREFERENCES_NAME",MODE_PRIVATE);
                        //edit()可以让对象处于编辑状态,用putXX保存值,并用commit提交,类似于数据库中的事物提交
                        sharedPreferences.edit().putString("userName",uName).putString("password",password).commit();
                    }else{
                        Toast.makeText(getApplicationContext(), "登陆成功", Toast.LENGTH_LONG).show();
                    }
                }else{
                    Toast.makeText(getApplicationContext(), "账号和密码不正确", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

}


运行效果


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: