数据存储——SharedPreferencesd的使用与登录

1.布局文件如下所示:loginlayout.xml

<?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"
    android:background="@drawable/drawable_rounder"
    android:padding="@dimen/activity_horizontal_margin">
    <EditText
        android:id="@+id/EtName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/etname"
        android:ems="10"
        android:drawablePadding="10dp"
        android:background="@android:drawable/edit_text"
        android:drawableLeft="@drawable/icon_user" >
    <requestFocus/>
    </EditText>
    <EditText
        android:id="@+id/EtPass"
        android:layout_below="@id/EtName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/etpass"
        android:drawablePadding="10dp"
        android:inputType="textPassword"
        android:ems="10"
        android:background="@android:drawable/edit_text"
        android:drawableLeft="@drawable/icon_pass">


    <requestFocus/>
    </EditText>
    <CheckBox
        android:id="@+id/cbpass"
        android:layout_below="@id/EtPass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:textSize="20dp"
        android:layout_margin="5dp"
        android:checked="false"/>
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_below="@id/cbpass">
         <Button
             android:id="@+id/btn"
             android:onClick="login"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="@string/de"
             android:layout_marginRight="10dp"
             android:background="@drawable/bt_press"
             />
         <Button
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="@string/zhuce"
             android:background="@drawable/bt_press"
             />
     </LinearLayout>

</RelativeLayout>

2.布局文件代码如下所示:activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@drawable/loginbg"
    android:id="@+id/activity_login"
    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="bzu.edu.cn.case_login.LoginActivity">


<include layout="@layout/loginlayout"/>


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/deer"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="23dp"
        android:layout_marginEnd="23dp"
        android:id="@+id/imageView" />
</RelativeLayout>

3.对布局背景的设置在drawable下新建loginbg.xml和loginbg_round.xml代码分别如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
   <gradient android:startColor="#FFACDAE5"
       android:endColor="#FF72CAE1"
       android:angle="45"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
      <corners android:radius="10dp"></corners>
      <solid android:color="#55FFFFFF"></solid>
</shape>

4.对按钮进行设置:在drawable下分别新建btn_press.xml、btn_shape.xml、btn_shape_after.xml,代码分别如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item  android:drawable="@drawable/btu_shape" android:state_pressed="false"></item>
     <item  android:drawable="@drawable/btu_shape_after" android:state_pressed="true"></item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp"></corners>
    <solid android:color="#FF72CAE1"></solid>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp"></corners>
    <solid android:color="#82cefa"></solid>
</shape>

5.loginActivity.Java

package bzu.edu.cn.case_login;



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 LoginActivity extends AppCompatActivity {
    private EditText Ename,Epass;
    private CheckBox cb;
    private Button btn;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //获取控件
        Ename=(EditText)findViewById(R.id.EtName);
        Epass=(EditText)findViewById(R.id.EtPass);
        cb=(CheckBox)findViewById(R.id.cbpass);


        //通过getSharedPreferences()方法获取SharePreferences的实例对象
        sharedPreferences=getSharedPreferences("Login",MODE_PRIVATE);


        //获取Editor实例对象,存储数据
        editor=sharedPreferences.edit();
        //获取数据
        String name=sharedPreferences.getString("name","");
        String pass=sharedPreferences.getString("pass","");
        //判断文本框是否为空
        if(name==null&&pass==null){
            cb.setChecked(false);
        }
        else{
            cb.setChecked(true);
            Ename.setText(name);
            Epass.setText(pass);
        }
    }
    public void login(View view){
        //获取文本框的内容
        String name = Ename.getText().toString().trim();
        String pass=Epass.getText().toString().trim();
        //检验用户名和密码是否正确
        if("admin".equals(name)&&"123456".equals(pass)){
            if(cb.isChecked()){
                //保存数据
                editor.putString("name",name);
                editor.putString("pass",pass);
                editor.commit();  //提交数据
            }else{
                editor.clear();  //删除所有数据
                editor.commit();  //提交修改
            }
            Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(this,"用户名或密码错误",Toast.LENGTH_LONG).show();
        }
    }
}

6.效果图展示如下:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值