登录界面
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=".StartLoginActivity">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入学号"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:textSize="20sp"/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:textSize="20sp"
/>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
/>
<Button
android:id="@+id/btn_register1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="还没有账号?点击注册"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
/>
</LinearLayout>
java
package com.example.schooltrade;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.schooltrade.db.UserDBOpenHelper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StartLoginActivity extends AppCompatActivity {
private Button btn_login,btn_register1;
private EditText et_password,et_userName;
private UserDBOpenHelper dbOpenHelper;
String dbpassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_login);
initView();
getSupportActionBar().setTitle("登录");
dbOpenHelper=new UserDBOpenHelper(StartLoginActivity.this,"user.db",null,1);
btn_register1=(Button) findViewById(R.id.btn_register1);
btn_register1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), StartRegisterActivity.class);
startActivity(intent);
}
});
btn_login=(Button) findViewById(R.id.btn_login);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String etpassword=et_password.getText().toString();
String key=et_userName.getText().toString();
Cursor cursor=dbOpenHelper.getReadableDatabase().query("user",null,"username = ?",new String[]{key},null,null,null);
ArrayList<Map<String,String>> resultList =new ArrayList<Map<String,String>>();
while(cursor.moveToNext()){
Map<String,String> map=new HashMap<>();
map.put("username",cursor.getString(1));
map.put("password",cursor.getString(2));
resultList.add(map);
dbpassword=map.get("password");
}
if(!isStudentNumber(key)){
Toast.makeText(StartLoginActivity.this,"请输入正确的学号!",Toast.LENGTH_SHORT).show();
}else if (resultList == null || resultList.size() == 0) {
Toast.makeText(StartLoginActivity.this,
"该用户名未注册,请先注册", Toast.LENGTH_LONG).show();
} else {
if(etpassword.equals(dbpassword)){
Toast.makeText(StartLoginActivity.this,"登陆成功!",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(StartLoginActivity.this,MainActivity.class);
startActivity(intent);
StartLoginActivity.this.finish();
}else{
Toast.makeText(StartLoginActivity.this,"密码错误!",Toast.LENGTH_SHORT).show();
}
};
}
});
}
public static boolean isStudentNumber(String num) {
if (num != null && num.length() == 10) {
Pattern pattern = Pattern.compile("^3[0-9][0-9]0[1-9][1-9][1-9][0|1|2|3]\\d{2}$");
Matcher matcher = pattern.matcher(num);
return matcher.matches();
}else{
return false;
}
}
private void initView(){
btn_login=findViewById(R.id.btn_login);
et_userName=findViewById(R.id.et_username);
et_password=findViewById(R.id.et_password);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (dbOpenHelper != null) {
dbOpenHelper.close();
}
}
}
注册
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=".StartRegisterActivity">
<EditText
android:id="@+id/et_register_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输学号"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/et_register_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/et_again_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请再次输入密码进行确认"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击注册"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
/>
</LinearLayout>
java
package com.example.schooltrade;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.schooltrade.db.UserDBOpenHelper;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StartRegisterActivity extends AppCompatActivity {
public static final int RESULT_CODE_REGISTER=0;
private Button btn_register;
private EditText et_register_username,et_register_password,et_again_password;
private UserDBOpenHelper dbOpenHelper;
String et_name;
String et_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_register);
getSupportActionBar().setTitle("注册");
btn_register=(Button) findViewById(R.id.btn_register);
et_register_username= findViewById(R.id.et_register_username);
et_register_password=findViewById(R.id.et_register_password);
et_again_password=findViewById(R.id.et_again_password);
dbOpenHelper=new UserDBOpenHelper(StartRegisterActivity.this,"user.db",null,1);
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String et_name=et_register_username.getText().toString();
String et_password=et_register_password.getText().toString();
String et_confirm=et_again_password.getText().toString();
if(TextUtils.isEmpty(et_name)){
Toast.makeText(StartRegisterActivity.this,"学号不能为空!",Toast.LENGTH_SHORT).show();
}else if(!isStudentNumber(et_name)){
Toast.makeText(StartRegisterActivity.this,"请输入正确的学号!",Toast.LENGTH_SHORT).show();
} else if(TextUtils.isEmpty(et_password)){
Toast.makeText(StartRegisterActivity.this,"密码不能为空!",Toast.LENGTH_SHORT).show();
}else if(!TextUtils.equals(et_password,et_confirm)){
Toast.makeText(StartRegisterActivity.this,"密码不一致!",Toast.LENGTH_SHORT).show();
} else{
insertData(dbOpenHelper.getReadableDatabase(),et_name,et_password);
Toast.makeText(StartRegisterActivity.this,"注册成功!",Toast.LENGTH_SHORT).show();
}
StartRegisterActivity.this.finish();
}
});
}
public static boolean isStudentNumber(String num) {
if (num != null && num.length() == 10) {
Pattern pattern = Pattern.compile("^3[0-9][0-9]0[1-9][1-9][1-9][0|1|2|3]\\d{2}$");
Matcher matcher = pattern.matcher(num);
return matcher.matches();
}else{
return false;
}
}
private void insertData(SQLiteDatabase readableDatabase, String username1, String password1){
ContentValues values=new ContentValues();
values.put("username",username1);
values.put("password",password1);
readableDatabase.insert("user",null,values);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (dbOpenHelper != null) {
dbOpenHelper.close();
}
}
}
Manifest
<activity android:name=".StartRegisterActivity" />
<activity
android:name=".StartLoginActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />