注意:实现数据库的完整版也实现啦,点击这里
文章目录
一、最终效果:
登录页面:
注册页面: (可以对输入的内容进行校验:比如不为空 且密码和再次输入的密码必须一致,若不一致,则会弹框说明情况)
代码目录:
二、代码部分
1.登录部分
1.LoginActivity.java
package com.example.mainapp_bleda2;import android.app.Activity;import android.nfc.Tag;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import androidx.annotation.Nullable;import androidx.appcompat.app.AppCompatActivity;
publicclassLoginActivity extends AppCompatActivity{
private Button btnLogin;
private EditText etAccount, etPassword;
private String userName="111";
private String pwd="123";@Override
protected void onCreate(@Nullable Bundle savedInstanceState){super.onCreate(savedInstanceState);
setContentView(R.layout.login);
btnLogin= findViewById(R.id.btn_login);
etAccount= findViewById(R.id.et_account);
etPassword= findViewById(R.id.et_password);//设置点击监听按钮
btnLogin.setOnClickListener(new View.OnClickListener(){@Override
public void onClick(View view){
String account= etAccount.getText().toString();
String password= etPassword.getText().toString();if(TextUtils.equals(account,userName)){if(TextUtils.equals(password,pwd)){
Toast.makeText(LoginActivity.this,"登录成功!",Toast.LENGTH_LONG).show();}else{
Toast.makeText(LoginActivity.this,"密码错误!",Toast.LENGTH_LONG).show();}}else{
Toast.makeText(LoginActivity.this,"用户名错误!",Toast.LENGTH_LONG).show();}}});}}
2.login.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=".LoginActivity">//logo和标题部分<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"
android:gravity="center_horizontal"><ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@mipmap/ic_launcher_foreground"/><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_vertical"
android:text="步态压力监测系统"
android:textSize="43sp"
android:layout_marginTop="-35dp"/></LinearLayout><!-- 账号框--><LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="50dp"><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:textSize="25dp"
android:gravity="center_vertical"/><EditText
android:id="@+id/et_account"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="请输入手机号"
android:textSize="18dp"
android:layout_marginLeft="20dp"
android:paddingLeft="10dp"
android:inputType="number"
android:background="@drawable/edit_text_bg"/></LinearLayout><!-- 密码--><LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="25dp"
android:gravity="center_vertical"/><EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="请输入密码"
android:textSize="18dp"
android:layout_marginLeft="20dp"
android:paddingLeft="10dp"
android:inputType="textPassword"
android:background="@drawable/edit_text_bg"/></LinearLayout><!-- 记住密码--><LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"><CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"/><CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录"
android:layout_marginLeft="40dp"/></LinearLayout><Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="25sp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textColor="@color/white"
android:background="@drawable/btn_bg_selector"/><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:text="还没有账号?"
android:layout_gravity="right"
android:layout_marginRight="2dp"
android:layout_marginTop="10dp"/></LinearLayout>
3.themes.xml
这个xml文件中放着一些主题色
<resources xmlns:tools="http://schemas.android.com/tools"><!-- 主题色--><color name="colorPrimary">@color/purple_500</color><color name="colorPrimaryVariant">@color/purple_700</color><!-- Base application theme.--><style name="Theme.SimpleAndroidApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar"><!-- Primary brand color.--><item name="colorPrimary">@color/purple_500</item><item name="colorPrimaryVariant">@color/purple_700</item><item name="colorOnPrimary">@color/white</item><!-- Secondary brand color.--><item name="colorSecondary">@color/teal_200</item><item name="colorSecondaryVariant">@color/teal_700</item><item name="colorOnSecondary">@color/black</item><!-- Status bar color.--><item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item><!-- Customize your themhere.--></style></resources>
2.注册部分
1.RegisterActivity.java
package com.example.mainapp_bleda2;import android.content.Intent;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;
publicclassRegisterActivity extends AppCompatActivity implements View.OnClickListener{
private EditText etAccount, etPass, etPassConfirm;
private Button btnRegister, button2;
private CheckBox cbAgree;@Override
protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);// 返回登录页面
button2=(Button) findViewById(R.id.button2);
etAccount= findViewById(R.id.et_account1);
etPass= findViewById(R.id.et_password1);
etPassConfirm= findViewById(R.id.et_password_confirm);
btnRegister= findViewById(R.id.btn_register);
cbAgree= findViewById(R.id.cb_Agree);
button2.setOnClickListener(new View.OnClickListener(){@Override
public void onClick(View view){//Intent intent= new Intent(MainActivity.this,DetailActivity.class);
Intent intent= new Intent(RegisterActivity.this,LoginActivity.class);
startActivity(intent);}});// 第一种方式写监听事件// btnRegister.setOnClickListener(new View.OnClickListener(){// @Override// public void onClick(View view){// …………//}//});// 第二种方式写监听事件
btnRegister.setOnClickListener(this);}@Override
public void onClick(View view){
String name= etAccount.getText().toString();
Stringpass= etPass.getText().toString();
String passConfirm= etPassConfirm.getText().toString();// TextUtils.isEmpty() 和 name.isEmpty() 的区别// 第一个可以判断 NULL 和 “” 两种为空的情况,且均判断为空// 第二个只能判断“”这一种情况,如果为NULL则会报空指针异常。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;}
Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_LONG).show();}}
2.activity_register.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=".RegisterActivity"><LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:gravity="left"><ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginLeft="-30dp"
android:layout_marginTop="-30dp"
android:src="@mipmap/ic_launcher_foreground"/><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_vertical"
android:layout_marginTop="-10dp"
android:layout_marginLeft="-33dp"
android:text="步态压力监测系统"
android:textSize="33sp"/></LinearLayout><!-- 账号框--><LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账  号:"
android:textSize="25dp"
android:gravity="center_vertical"/><EditText
android:id="@+id/et_account1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="请输入手机号"
android:textSize="18dp"
android:layout_marginLeft="20dp"
android:paddingLeft="10dp"
android:inputType="number"
android:background="@drawable/edit_text_bg"/></LinearLayout><!-- 密码--><LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密  码:"
android:textSize="25dp"
android:gravity="center_vertical"/><EditText
android:id="@+id/et_password1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="请输入密码"
android:textSize="18dp"
android:layout_marginLeft="20dp"
android:paddingLeft="10dp"
android:inputType="textPassword"
android:background="@drawable/edit_text_bg"/></LinearLayout><!-- 确认密码--><LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认密码:"
android:textSize="25dp"
android:gravity="center_v