Android development--realize the Android login registration page (below)

We have completed the layout of the login registration page before, and now we will implement the functions of verifying login and remembering passwords.

We haven't used the database here yet, so our verification account password is hard-coded.

First enter the login page, and you can jump to the registration page from here. After the registration is successful, the account password input box will automatically obtain the account password just registered, and there is no need to enter it again. After the login is successful, the page jumps to the homepage, and the homepage obtains and displays the account just registered, and clicks logout on the homepage to return to the login page.

    

Next, first write the content of the homepage activity_main.xml page:

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="欢迎!"
        android:textSize="40sp"
        android:layout_marginTop="30dp"
        android:layout_gravity="center_horizontal"></TextView>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="退出登录"
        android:textSize="25sp"
        android:layout_margin="20dp"
        style="@style/MyBtnStyle"
        android:onClick="loginOut"></Button>
</LinearLayout>

The effect is as shown in the figure:

The MainActivity.java code of the home page is as follows:

public class MainActivity extends AppCompatActivity {

    private TextView tvContent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setTitle("首页");

        tvContent=findViewById(R.id.tv_content);
        Intent intent=getIntent();
        String account=intent.getStringExtra("account");
        tvContent.setText("欢迎你:"+account);
    }

    //退出登录按钮点击事件
    public void loginOut(View view) {
        Intent intent=new Intent(this,LoginActivity.class);
        startActivity(intent);
        this.finish();
    }
}

The code here is the content that contains the content of verifying the login and remembering the password.

First is the LoginActivity.java page:

public class LoginActivity extends AppCompatActivity {

    public static final int REQUEST_CODE_REGISTER = 1;
    private static final String TAG="tag";
    private Button btnLogin;
    private EditText etAccount,etPassword;
    private CheckBox cbRemember;
    private String userName="a";
    private String pass="123";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getSupportActionBar().setTitle("登录");

        initView();
        initData();


        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断账号密码是否符合要求
                String account=etAccount.getText().toString();
                String password=etPassword.getText().toString();
                if (TextUtils.isEmpty(userName)){
                    Toast.makeText(LoginActivity.this,"还没有注册账号!",Toast.LENGTH_LONG).show();
                    return;
                }
                if (TextUtils.equals(account,userName)){
                    if (TextUtils.equals(password,pass)){
                        Toast.makeText(LoginActivity.this,"恭喜你,登录成功!",Toast.LENGTH_LONG).show();
                        if (cbRemember.isChecked()){
                            SharedPreferences spf=getSharedPreferences("spfRecorid",MODE_PRIVATE);
                            SharedPreferences.Editor edit=spf.edit();
                            edit.putString("account",account);
                            edit.putString("password",password);
                            edit.putBoolean("isRemember",true);
                            edit.apply();
                        }else {
                            SharedPreferences spf=getSharedPreferences("spfRecorid",MODE_PRIVATE);
                            SharedPreferences.Editor edit=spf.edit();
                            edit.putBoolean("isRemember",false);
                            edit.apply();
                        }

                        //实现跳转
                        Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                        //传递用户名
                        intent.putExtra("account",account);
                        startActivity(intent);
                        //接收自己
                        LoginActivity.this.finish();

                    }else {
                        Toast.makeText(LoginActivity.this,"密码错误!",Toast.LENGTH_LONG).show();
                    }
                }else {
                    Toast.makeText(LoginActivity.this,"用户名错误",Toast.LENGTH_LONG).show();
                }

            }
        });
    }

    //记住密码(取出数据)
    private void initData() {
        SharedPreferences spf=getSharedPreferences("spfRecorid",MODE_PRIVATE);
        boolean isRemember=spf.getBoolean("isRemember",false);
        String account=spf.getString("account","");
        String password=spf.getString("password","");
        //更新用户名密码(注册的用户名密码)
        userName=account;
        pass=password;

        if (isRemember){
            etAccount.setText(account);
            etPassword.setText(password);
            cbRemember.setChecked(true);
        }

    }

    //初始化
    private void initView(){
        btnLogin=findViewById(R.id.btn_login);
        etAccount=findViewById(R.id.et_account);
        etPassword=findViewById(R.id.et_password);
        cbRemember=findViewById(R.id.cb_remember);
    }

    //还没有账号(跳转到注册)
    public void toRegister(View view) {
        Intent intent=new Intent(this,RegisterActivity.class);
        
        //startActivity(intent);
        startActivityForResult(intent,REQUEST_CODE_REGISTER);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==REQUEST_CODE_REGISTER&&resultCode==RegisterActivity.RESULT_CODE_REGISTER&&data!=null){
            Bundle extras=data.getExtras();
            //获取用户密码
            String account=extras.getString("account","");
            String password=extras.getString("password","");
            etAccount.setText(account);
            etPassword.setText(password);

            //更新用户名密码(注册的用户名密码)
            userName=account;
            pass=password;
        }
    }
}

Next is the content of the RegisterActivity.java page

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    public static final int RESULT_CODE_REGISTER = 0;
    private Button btnRegister;
    private EditText etAccount,etPass,etPassConfirm;
    private CheckBox cbAgree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        getSupportActionBar().setTitle("注册");

        etAccount=findViewById(R.id.et_account);
        etPass=findViewById(R.id.et_password);
        etPassConfirm=findViewById(R.id.et_password_Confirm);
        cbAgree=findViewById(R.id.cb_agree);
        btnRegister=findViewById(R.id.btn_register);

        btnRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String name=etAccount.getText().toString();
        String pass=etPass.getText().toString();
        String PassConfirm=etPassConfirm.getText().toString();

        if (TextUtils.isEmpty(name)){
            Toast.makeText(RegisterActivity.this,"用户名不能为空!",Toast.LENGTH_LONG).show();
            return;
        }
        if (TextUtils.isEmpty(pass)){
            Toast.makeText(RegisterActivity.this,"密码不能为空!",Toast.LENGTH_LONG).show();
            return;
        }
        if (!TextUtils.equals(pass,PassConfirm)){
            Toast.makeText(RegisterActivity.this,"密码不一致!",Toast.LENGTH_LONG).show();
            return;
        }
        if (!cbAgree.isChecked()){
            Toast.makeText(RegisterActivity.this,"请同意用户协议!",Toast.LENGTH_LONG).show();
            return;
        }
        //存储注册的用户名和密码
        SharedPreferences spf=getSharedPreferences("spfRecorid",MODE_PRIVATE);
        SharedPreferences.Editor edit = spf.edit();
        edit.putString("account",name);
        edit.putString("password",pass);
        //注册成功后,回到登录页面,数据回传
        Intent intent=new Intent();
        Bundle bundle=new Bundle();
        bundle.putString("account",name);
        bundle.putString("password",pass);
        intent.putExtras(bundle);
        setResult(RESULT_CODE_REGISTER,intent);
        Toast.makeText(RegisterActivity.this,"注册成功!",Toast.LENGTH_LONG).show();
        this.finish();
    }
}

At this point, the functions of verifying login and remembering password are completed! congratulations!

The above codes are all coded by myself one by one, and I have also run them, and the results are no problem. Of course, if you have any suggestions, please let me know❥(^_-)

The specific source code is shared in the comment area, and those who need it can pick it up by themselves.

Guess you like

Origin blog.csdn.net/Waterme10n/article/details/122644194