SharedPrefences

一.存储方式

SharedPreferences 中存储的数据是以键值对的形式保存在XML文件中。

二.获取实例对象的方法

1.通过context.getSharedPreferences(Stringname,int mode)获取

2.Activity中的getPreferences()方法

3.PrefencesManager类中的getDefauHSharedPerferences()获取

三.实例---登录

1.Login_top

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="230dp"
        android:alpha="0.8"
        android:background="@drawable/shape" />

    <LinearLayout
        android:id="@+id/input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="vertical">

        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:drawableLeft="@drawable/icon_user"
            android:hint="Please input your UserName"
            android:textSize="15sp" />

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="5dp"
            android:drawableLeft="@drawable/icon_pass"
            android:hint="Please input your password"
            android:inputType="textPassword"
            android:textSize="15sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/input"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码"
            android:paddingRight="50dp"
            />

        <Button
            android:id="@+id/bt_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_selector"
            android:text="Login" />
    </LinearLayout>
</RelativeLayout>
2.info.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:textSize="50dp"
        android:layout_centerInParent="true"
        />

</RelativeLayout>
3.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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.bz0209.login.MainActivity"
    android:background="@drawable/bg1"
    >

    <TextView
        android:onClick="text"
        android:textSize="15sp"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/include"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Forget your Password?"
        />
    <ImageView
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@drawable/deer"/>

    <include
        android:id="@+id/include"
        layout="@layout/login_top"
         />


</RelativeLayout>
四.MainActivity
package com.example.bz0209.login;


import android.content.Context;
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.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button login=null;
    private EditText uName=null;
    private EditText uPsw=null;
    private CheckBox checkBoxBtn=null;
    SharedPreferences sp=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sp=this.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        init();
    }

    private void init() {
        uName=(EditText) findViewById(R.id.username);
        uPsw=(EditText) findViewById(R.id.password);
        checkBoxBtn=(CheckBox) findViewById(R.id.checkBox);
        login=(Button) findViewById(R.id.bt_login);
        if (sp.getBoolean("checkboxBoolean",false)){
            uName.setText(sp.getString("uName",null));
            uPsw.setText(sp.getString("uPsw",null));
            checkBoxBtn.setChecked(true);
        }
        login.setOnClickListener(this);
    }

    public void onClick(View view){
        if (view==login){
            String name=uName.getText().toString();
            String psw=uPsw.getText().toString();
            if (name.trim().equals("")){
                Toast.makeText(this,"请输入用户名!",Toast.LENGTH_SHORT).show();
                return;
            }
            if (psw.trim().equals("")){
                Toast.makeText(this,"请输入密码!",Toast.LENGTH_SHORT).show();
                return;
            }
            boolean CheckBoxLogin=checkBoxBtn.isChecked();
            if (CheckBoxLogin){
                SharedPreferences.Editor editor=sp.edit();
                editor.putString("uName",name);
                editor.putString("uPsw",psw);
                editor.putBoolean("checkboxBoolean",true);
                editor.commit();
            }else {
                SharedPreferences.Editor editor=sp.edit();
                editor.putString("uName",null);
                editor.putString("uPsw",null);
                editor.putBoolean("checkboxBoolean",false);
                editor.commit();
            }
            Intent intent=new Intent(MainActivity.this,info.class);
            startActivity(intent);
            finish();
        }
    }
}
五.展示
 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值