Android实现登录注册页面(下)
前面我们已经完成了登录注册页面的布局,下面我们实现验证登录和记住密码的功能。
我们这里还没用到数据库,所以我们的验证的账号密码,是写死的。
首先进入登录页面,可以从这里跳转到注册页面,注册成功后,账号密码的输入框会自动获取刚刚注册的账号密码,无需再次输入。登录成功后,页面跳转到首页,首页获取并显示刚刚注册的账号,点击首页的退出登录,则返回到登录页面。
接下来,先写首页activity_main.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" 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>
效果如图:
首页的MainActivity.java代码如下:
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(); } }
这里的代码是包含验证登录的内容和记住密码的内容。
首先是LoginActivity.java 页面:
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; } } }
接下来是RegisterActivity.java 页面的内容
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(); } }
到这里,验证登录和记住密码的功能就完成啦!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)