数据存储

 layout
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    tools:context="com.gemptc.sharedpreferences.MainActivity">


    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名:"
        />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="请输入密码:"
        />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:onClick="login"
            android:text="登录"/>

        <CheckBox
           android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="记住密码"/>
    </RelativeLayout>

</LinearLayout>

 success.xml
 main.java
package com.gemptc.sharedpreferences;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.gemptc.myapplication0503morning.R;

public class MainActivity extends AppCompatActivity {


    public static final String SAVE = "save";
    EditText mUserNameEditText,mPassWordEditText;
    String mUserNameString,mPassWordString;
    CheckBox mCheckBox;
    SharedPreferences.Editor mEditor;
    SharedPreferences mSharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        adjustIsLogin();
        initViews();
        initListeners();
    }

    private void adjustIsLogin() {
        mSharedPreferences=getSharedPreferences(SAVE,MODE_PRIVATE);
        //取出偏好设置文件中的值,第一个参数,数据的键值,第二个参数表示此键值不存在时候的默认值
        boolean isLogin=mSharedPreferences.getBoolean("islogin",false);
        if (isLogin){
            //已经记住了用户名和密码,直接跳转到成功界面
            Intent intent=new Intent(MainActivity.this,SuccessActivity.class);
            mUserNameString=mSharedPreferences.getString("username","");
            intent.putExtra("name",mUserNameString);
            startActivity(intent);
            finish();
        }
    }

    private void initListeners() {
       /* mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    //记住用户名
                    if (mUserNameString.equals("123")&&mPassWordString.equals("123")){
                        mSharedPreferences=getSharedPreferences(SAVE,MODE_PRIVATE);
                         //

                    }
                }else{
                    //取消记住
                }
            }
        });*/
    }
    //记住用户名和密码不再由复选框单击事件触发,而是由登录按钮触发:单击登录按钮时,判断复选框是否被选中
    private void initViews() {
        mUserNameEditText= (EditText) findViewById(R.id.username);
        mPassWordEditText= (EditText) findViewById(R.id.password);
        mCheckBox= (CheckBox) findViewById(R.id.checkbox);
    }

    public void login(View view) {
        mUserNameString=mUserNameEditText.getText().toString();
        mPassWordString=mPassWordEditText.getText().toString();

        //判断用户是否选中了复选框
        if (mCheckBox.isChecked()) {
            //用户选择记住用户名和密码
            if (mUserNameString.equals("123") && mPassWordString.equals("123")) {
                //第一个参数:偏好设置文件的名称;第二个参数:文件访问模式
                mSharedPreferences = getSharedPreferences(SAVE, MODE_PRIVATE);
                //向偏好设置文件中保存数据
                mEditor = mSharedPreferences.edit();
                mEditor.putString("username", mUserNameString);
                mEditor.putBoolean("islogin", true);
                //提交保存结果
                mEditor.commit();
            } else {
                //用户没有选中复选框,需要清空
                //第一个参数:偏好设置文件的名称;第二个参数:文件访问模式
                mSharedPreferences = getSharedPreferences(SAVE, MODE_PRIVATE);
                //向偏好设置文件中保存数据
                mEditor = mSharedPreferences.edit();
                mEditor.putString("username", mUserNameString);
                mEditor.putBoolean("islogin", false);
                //提交保存结果
                mEditor.commit();

            }
        } else {
            //用户没有选中复选框,需要清空
            //第一个参数:偏好设置文件的名称;第二个参数:文件访问模式
            mSharedPreferences = getSharedPreferences(SAVE, MODE_PRIVATE);
            //向偏好设置文件中保存数据
            mEditor = mSharedPreferences.edit();
            mEditor.putString("username", mUserNameString);
            mEditor.putBoolean("islogin", false);
            //提交保存结果
            mEditor.commit();
        }
        if (mUserNameString.equals("123")&&mPassWordString.equals("123")){
            //登录成功
            Intent intent=new Intent(MainActivity.this,SuccessActivity.class);
            intent.putExtra("name",mUserNameString);
            startActivity(intent);

        }else {
            //登录失败
            //清空用户名和密码,设为空
            mUserNameEditText.setText("");
            mPassWordEditText.setText("");
            //把光标移开始处
            mUserNameEditText.requestFocus();
        }

    }
}





  
  success.java
  package com.gemptc.sharedpreferences;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.gemptc.myapplication0503morning.R;

public class SuccessActivity extends AppCompatActivity {
    TextView mTextView;
    String mName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_success);
        initData();
        initViews();
    }

    private void initViews() {
        mTextView= (TextView) findViewById(R.id.success);
        mTextView.setText(mName);

    }

    private void initData() {
        Intent intent=getIntent();
        mName=intent.getStringExtra("name");
    }
}

你可能感兴趣的:(数据存储)