拥有记住密码功能的登录界面

LoginActivity

package com.example.sharedpreferences;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
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 login_edit_account;
    private EditText login_edit_password;
    private Button login_butt_login;
    //
    private CheckBox login_check_rempass;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    //
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        login_edit_account=findViewById(R.id.main_edit_account);
        login_edit_password=findViewById(R.id.main_edit_password);
        login_butt_login=findViewById(R.id.main_butt_login);
        //
        preferences= PreferenceManager.getDefaultSharedPreferences(this);
        login_check_rempass=findViewById(R.id.login_chect_rememberpass);
        //判断是不是需要记住密码
        boolean isRemember=preferences.getBoolean("remember_password",false);
        if(isRemember){
            //将账号和密码都设置到文本框中
            String account=preferences.getString("account","");
            String password=preferences.getString("password","");
            login_edit_account.setText(account);
            login_edit_password.setText(password);
            login_check_rempass.setChecked(true);
        }
        login_butt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account=login_edit_account.getText().toString();
                String password=login_edit_password.getText().toString();
                //如果账号是zhaixiaohua,密码是123456,就认为是登录成功
                if(account.equals("zhaixiaohua")&&password.equals("123456")){
                    //记住密码部分
                    editor=preferences.edit();
                    //检查复选框是否被选中
                    if(login_check_rempass.isChecked()){
                        editor.putBoolean("remember_password",true);
                        editor.putString("account",account);
                        editor.putString("password",password);

                    }else{
                        editor.clear();
                    }
                    editor.apply();
                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(LoginActivity.this,"账号或密码错误",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  >
  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="60dp"
      android:orientation="horizontal">
      <TextView
          android:layout_width="90dp"
          android:layout_height="wrap_content"
          android:layout_gravity="center_vertical"
          android:textSize="18sp"
          android:text="Account"/>
      <EditText
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:id="@+id/main_edit_account"
          android:layout_weight="1"
          android:layout_gravity="center_vertical"/>

  </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textSize="18sp"
            android:text="password"/>
        <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/main_edit_password"
            android:layout_weight="1"
        android:layout_gravity="center_vertical"

        android:inputType="textPassword"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/login_chect_rememberpass"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="Remember password"/>
    </LinearLayout>
        <Button
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:id="@+id/main_butt_login"
            android:text="Login"/>

</LinearLayout>

MainActivity

package com.example.sharedpreferences;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button main_bu_cun;
    private Button main_bu_du;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_bu_cun=findViewById(R.id.main_bu_cun);
        main_bu_du=findViewById(R.id.main_bu_duqu);
        //存数据
        main_bu_cun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor=getSharedPreferences("data",MODE_PRIVATE).edit();
                editor.putString("name","tom");
                editor.putInt("age",28);
                editor.putBoolean("married",false);
                editor.apply();
            }
        });
        //读取数据
        main_bu_du.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences preferences=getSharedPreferences("data",MODE_PRIVATE);
                String name=preferences.getString("name","");
                int age=preferences.getInt("age",0);
                Boolean married=preferences.getBoolean("married",false);
                Log.d("MainActivity","name is"+name);
                Log.d("MainActivity", "age is"+age);
                Log.d("MainActivity", "maaried is"+married);

            }
        });
    }
}

activity_main.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="match_parent"
    android:orientation="horizontal">

   <Button
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/main_bu_cun"
       android:text="save data"/>
   <Button
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@+id/main_bu_cun"
       android:id="@+id/main_bu_duqu"
       android:text="restore data"
       />



</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值