第5天sp存储案例:记住密码+自动登录

第5天sp存储案例:记住密码+自动登录

记住密码+自动登录

(1)xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity"
    android:orientation="vertical"
    android:gravity="center">
    <EditText
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <RelativeLayout
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <CheckBox
            android:layout_alignParentLeft="true"
            android:id="@+id/cb_remember"
            android:text="记住密码"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
            <CheckBox
            android:layout_alignParentLeft="true"
            android:id="@+id/cb_auto_login"
            android:text="自动登录"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:layout_alignParentRight="true"
            android:id="@+id/login"
            android:text="登录"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
</LinearLayout>

(2)Java代码

public class LoginActivity extends AppCompatActivity {
    private SharedPreferences sharedPreferences;
    private EditText username;
    private EditText password;
    private CheckBox remember_cb;
    private CheckBox auto_cb;
    private Button login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        remember_cb=findViewById(R.id.cb_remember);
        auto_cb=findViewById(R.id.cb_auto_login);
        login=(Button)findViewById(R.id.login);
        //TODO 读取
        sharedPreferences=getSharedPreferences("1609A",MODE_PRIVATE);
        boolean ischeck= sharedPreferences.getBoolean("ischeck",false);
        boolean isauto= sharedPreferences.getBoolean("isauto",false);
        if(ischeck){
            //读到用户名和密码展现在页面中,复选框被勾选
            String username1=sharedPreferences.getString("username","");
            String password1=sharedPreferences.getString("password","");
            username.setText(username1);
            password.setText(password1);
            cb.setChecked(true);
        }
        if(isauto){//自动登录被选中
              Intent intent= new Intent(MainActivity.this,Main2Activity2.class);
              startActivity(intent);
        }
        
        //TODO 写数据
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username2=username.getText().toString().trim();
                String password2=password.getText().toString().trim();
                //用户名和密码是否正确
                if("songdingxing".equals(username2)&&"123456".equals(password2)){
                    //判断记住密码是否被选中
                    if(cb_remember.isChecked()){//存储数据
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        edit.putBoolean("ischeck",true);
                        edit.putString("username",username2);
                        edit.putString("password",password2);
                        edit.commit();
                    }else{//更新ischecked为false
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        edit.putBoolean("ischeck",false);
                        edit.commit();
                    }
                    //判断自动登录是否被选中
                    if(cb_auto.isChecked()){//存储数据
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        edit.putBoolean("isauto",true);
                        edit.commit();
                    }else{//更新isauto为false
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        edit.putBoolean("isauto",false);
                        edit.commit();
                    }
                    Intent intent= new Intent(MainActivity.this,Main2Activity2.class);
                    startActivity(intent);
                }
            }
        }); 
    }
}

猜你喜欢

转载自blog.csdn.net/qq_34178710/article/details/85099468