模仿登陆,记住密码

实验效果图
输入账号密码选择记住密码图
退出后重新进入项目效果图
退出后重新进入项目效果图
开发步骤
1.建立UI界面

 <TextView
        android:id="@+id/tvUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/tvName"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etUsername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvUsername"
        android:layout_below="@+id/tvUsername"
        android:background="@android:drawable/edit_text"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/tvPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etUsername"
        android:layout_below="@+id/etUsername"
        android:text="@string/tvPassword"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvPassword"
        android:layout_below="@+id/tvPassword"
        android:layout_marginTop="16dp"
        android:background="@android:drawable/edit_text"
        android:ems="10"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/etPassword"
        android:layout_below="@+id/etPassword"
        android:layout_marginTop="20dp"
        android:background="#FF72CAE1"
        android:onClick="save"
        android:text="@string/btnLogin" />

    <CheckBox
        android:id="@+id/cbKeeppsd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etPassword"
        android:layout_alignTop="@+id/btnLogin"
        android:layout_marginLeft="2dp"
        android:textSize="22dp"
        android:checked="true"
        android:text="@string/keeppsd" />
<TextView
        android:id="@+id/tvRegist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="21dp"
        android:layout_marginTop="18dp"
        android:text="@string/tvRegister"
        android:autoLink="all"
        android:textColorLink="#FF0066CC" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="24dp"
        android:src="@drawable/panda" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="28dp"
        android:src="@drawable/icon" />

2.美化布局界面

<?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" >
    <solid android:color="#55FFFFFF"/>
    <corners android:radius="10dp"/>
</shape>

3.SharedPreferences 用来保存数据

3.1 获取.SharedPreferences对象

preferences = getSharedPreferences("data", MODE_PRIVATE);

3.2 存入数据

cbKeeppsd.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                if (cbKeeppsd.isChecked()) {
                    preferences.edit().putBoolean("rem_pwd", true).commit();
                } else {
                    preferences.edit().putBoolean("rem_pwd", false).commit();
                }

            }

        });

3.3 判断账号密码是否正确

if (userName.equals("zhangsan") && password.equals("123")) {
                    Toast.makeText(MainActivity.this, "登录成功",
                            Toast.LENGTH_SHORT).show();

3.4记住账号密码

if (cbKeeppsd.isChecked()) {
                        Editor editor = preferences.edit();
                        editor.putString("USER_NAME", userName);
                        editor.putString("PASSWORD", password);
                        editor.commit();
                    }

3.5 界面跳转

Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                    intent.putExtra("name", userName);
                    startActivity(intent);

3.6 重新登录时 读取数据

public void read() {
        if (preferences.getBoolean("rem_pwd", false)) {
            cbKeeppsd.setChecked(true);
            etUserName.setText(preferences.getString("USER_NAME", ""));
            etPassword.setText(preferences.getString("PASSWORD", ""));
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值