一、activity_main.XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="青少年编程教育平台移动端"
android:gravity="center"
android:textSize="20dp"
android:textStyle="bold"/>
</androidx.constraintlayout.widget.ConstraintLayout>
二、activity_login.XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@mipmap/loginbg"
tools:context=".MainActivity">
<TextView
android:id="@+id/font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="288dp"
android:gravity="center"
android:text="欢迎登录"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/username"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintWidth_percent=".8"
app:layout_constraintVertical_bias=".5"
app:layout_constraintHorizontal_bias=".5"
android:hint="请输入用户名"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="@drawable/edit_text_bg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintWidth_percent=".8"
app:layout_constraintVertical_bias=".6"
app:layout_constraintHorizontal_bias=".5"
android:hint="请输入密码"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="@drawable/edit_text_bg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintWidth_percent=".8"
app:layout_constraintVertical_bias=".8"
android:text="登 录"
android:textSize="16sp"
android:textStyle="bold"
android:background="@drawable/button_bg"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/text_sign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="立即注册"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/password"
app:layout_constraintHorizontal_bias="0.137"
app:layout_constraintLeft_toLeftOf="@+id/password"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忘记密码?"
app:layout_constraintVertical_bias="0"
app:layout_constraintHorizontal_bias="1"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/password"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password"/>
</androidx.constraintlayout.widget.ConstraintLayout>
三、activity_register.XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@mipmap/loginbg">
<TextView
android:id="@+id/font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="288dp"
android:gravity="center"
android:text="欢迎注册"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/username"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintWidth_percent=".8"
app:layout_constraintVertical_bias=".5"
app:layout_constraintHorizontal_bias=".5"
android:hint="请输入用户名"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="@drawable/edit_text_bg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintWidth_percent=".8"
app:layout_constraintVertical_bias=".6"
app:layout_constraintHorizontal_bias=".5"
android:hint="请输入密码"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="@drawable/edit_text_bg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/confirmpwd"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintWidth_percent=".8"
app:layout_constraintVertical_bias=".7"
app:layout_constraintHorizontal_bias=".5"
android:hint="请再次输入密码"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="@drawable/edit_text_bg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_sign"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintWidth_percent=".8"
app:layout_constraintVertical_bias=".9"
android:text="注 册"
android:textSize="16sp"
android:textStyle="bold"
android:background="@drawable/button_bg"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
四、activity_main_page.XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".activity.MainPageActivity">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="主页"
android:gravity="center"
android:textSize="20dp"
android:textStyle="bold"/>
</androidx.constraintlayout.widget.ConstraintLayout>
五、MainActivity
public class MainActivity extends BaseActivity {
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = findViewById(R.id.text);
text.setOnClickListener((v)->{
// Intent in = new Intent(MainActivity.this, LoginActivity.class);
// startActivity(in);
navigateTo(LoginActivity.class);
});
}
}
六、LoginActivity
package com.zgf.program.activity;
public class LoginActivity extends BaseActivity {
private EditText username;
private EditText password;
private TextView text;
private Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
text = findViewById(R.id.text_sign);
text.setOnClickListener((v)->{
// Intent in = new Intent(LoginActivity.this, RegisterActivity.class);
// startActivity(in);
navigateTo(RegisterActivity.class);
});
btnLogin = findViewById(R.id.btn_login);
btnLogin.setOnClickListener((v)->{
// Intent in = new Intent(LoginActivity.this, RegisterActivity.class);
// startActivity(in);
String name = username.getText().toString().trim();
String pwd = password.getText().toString().trim();
login(name,pwd);
});
}
private void login(String name ,String pwd){
if(StringUtils.isEmpty(name)){
// Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT).show();
showToast("请输入账号");
return;
}
if (StringUtils.isEmpty(pwd)){
// Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
showToast("请输入密码");
return;
}
navigateTo(MainPageActivity.class);
}
}
七、RegisterActivity
package com.zgf.program.activity;
public class RegisterActivity extends BaseActivity {
private EditText username;
private EditText password;
private EditText confirmPwd;
private Button btnSign;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
confirmPwd = findViewById(R.id.confirmpwd);
btnSign = findViewById(R.id.btn_sign);
btnSign.setOnClickListener((v)->{
// Intent in = new Intent(RegisterActivity.this, RegisterActivity.class);
// startActivity(in);
String name = username.getText().toString().trim();
String pwd = password.getText().toString().trim();
String conpwd = confirmPwd.getText().toString().trim();
register(name,pwd,conpwd);
});
}
private void register(String name, String pwd,String conpwd) {
if(StringUtils.isEmpty(name)){
showToast("请输入账号");
return;
}
if (StringUtils.isEmpty(pwd)){
showToast("请输入密码");
return;
}
if (StringUtils.isEmpty(conpwd)){
showToast("请再次输入密码");
return;
}
if (pwd != conpwd){
showToast("两次输入的密码不一致,请重新输入");
return;
}
navigateTo(RegisterActivity.class);
}
}
八、MainPageActivity
public class MainPageActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
}
}
九、BaseActivity
public class BaseActivity extends AppCompatActivity {
public Context mContext;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mContext = this;
}
public void showToast(String msg){
Toast.makeText(mContext,msg,Toast.LENGTH_SHORT).show();
}
public void navigateTo(Class cls){
Intent in = new Intent(mContext, cls);
startActivity(in);
}
}
十、StringUtils
public class StringUtils {
public static boolean isEmpty(String str){
if(str == null || str.length() <= 0){
return true;
}else {
return false;
}
}
}