每日打卡APP,第一次作业

  本次为第一次个人作业第一阶段第二阶段项目展示。

  首先,项目结构如下:

以下为各文件代码:

1.UserDao

package com.example.notebook.Dao;

import android.os.Handler;

import com.example.notebook.enity.Card;
import com.example.notebook.enity.User;
import com.example.notebook.util.Commonutil;
import com.example.notebook.util.MysqlHelper;
import com.example.notebook.util.SqliteHelper;

import java.util.ArrayList;
import java.util.List;

import static android.os.Looper.getMainLooper;

public class UserDao extends MysqlHelper {
private static String conDay;
private SqliteHelper mHelper;
private static Handler mainHandler = new Handler(getMainLooper());; //主线程
/*
*添加用户信息
*/
public static int addUser(User item){
int iRow = 0;
try{
getConnection();
String sql = "insert into userinfo (username, phone, class, password) values(?,?,?,?)";
pStmt = conn.prepareStatement(sql);
pStmt.setString(1,item.getUsername());
pStmt.setString(2,item.getPhone());
pStmt.setString(3,item.getUclass());
pStmt.setString(4,item.getPassword());
iRow = pStmt.executeUpdate();

}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return iRow;
}

/*
*按学号和密码查询用户信息
*/
public static User getUserByUidAndUpass(String uid, String upass){
User item = null;
try{
getConnection();
String sql = "select * from userinfo where uid=? and password=?";
pStmt = conn.prepareStatement(sql);
pStmt.setString(1,uid);
pStmt.setString(2,upass);
rs = pStmt.executeQuery();
if(rs.next()){
item = new User();
item.setUid(rs.getInt("uid"));
item.setUsername(rs.getString("username"));
item.setPhone(rs.getString("phone"));
item.setUclass(rs.getString("class"));
item.setPassword(rs.getString("password"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return item;
}

/*
*按学号查询打卡信息
*/
public static List<Card> getCardByUid(String uid){
Card item = null;
ArrayList<Card> items = new ArrayList<>();
try{
int i = 0;
getConnection();
String sql = "select * from card where uid=?";
pStmt = conn.prepareStatement(sql);
pStmt.setString(1,uid);
rs = pStmt.executeQuery();
if(rs.next()){
item = new Card();
item.setUid(rs.getInt("uid"));
item.setDate(rs.getString("Date"));
item.setKeyvalue(rs.getString("keyvalue"));
item.setZongjie(rs.getString("zongjie"));
items.add(item);
i+=1;
}
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return items;
}

/*
*按学号查询打卡坚持天数
*/
public static int getConDayByUid(String uid){
Card item = null;
int i = 0;
try{
getConnection();
String sql = "select * from card where uid=?";
pStmt = conn.prepareStatement(sql);
pStmt.setString(1,uid);
rs = pStmt.executeQuery();
while(rs.next()){
item = new Card();
item.setUid(rs.getInt("uid"));
item.setDate(rs.getString("Date"));
item.setKeyvalue(rs.getString("keyvalue"));
item.setZongjie(rs.getString("zongjie"));
i+=1;
}
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return i;
}

/*
*按学号查询打卡最长连续天数
*/
public static int getMaxByUid(String uid){
Card item = null;
int i = 0;
int max = 0;
int temp = 0;
try{
getConnection();
String sql = "select " +
" uid" +
" ,day_cha " +
" ,count(date) flag_days " +
"from " +
" (select " +
" uid " +
" ,date " +
" ,date_rank " +
" ,(date_format(date,\"%e\") - date_rank) as day_cha " +
"from " +
" ( " +
" select " +
" uid " +
" ,date " +
" ,row_number() over(partition by uid order by date) date_rank " +
" from " +
" card " +
"where uid=?" +
" )t1 " +
" )t2 " +
"group by " +
" uid " +
" ,day_cha";
pStmt = conn.prepareStatement(sql);
pStmt.setString(1,uid);
rs = pStmt.executeQuery();
while(rs.next()){
temp=Integer.parseInt(rs.getString("flag_days"));
if(max<=temp) max=temp;
}
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return max;
}

/*
*按学号查询后比较今天是否打过卡
*/
public static boolean CompareDate(String uid){
Card item = null;
int i = 0;
boolean flag = false;
String nowdate = Commonutil.getNowDate();
try{
getConnection();
String sql = "select * from card where uid=? and Date=?";
pStmt = conn.prepareStatement(sql);
pStmt.setString(1,uid);
pStmt.setString(2,nowdate);
rs = pStmt.executeQuery();
if (rs.next()){
flag=true;
}
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return flag;
}

/*
*按用户名和密码查询用户信息
*/
public static User getUserByUnameAndUpass(String uname, String upass){
User item = null;
try{
getConnection();
String sql = "select * from userinfo where username=? and password=?";
pStmt = conn.prepareStatement(sql);
pStmt.setString(1,uname);
pStmt.setString(2,upass);
rs = pStmt.executeQuery();
if(rs.next()){
item = new User();
item.setUid(rs.getInt("uid"));
item.setUsername(rs.getString("username"));
item.setPhone(rs.getString("phone"));
item.setUclass(rs.getString("class"));
item.setPassword(rs.getString("password"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return item;
}

/*
*添加打卡信息
*/
public static int addCard(Card item){
int iRow = 0;
try{
getConnection();
String sql = "insert into card (uid, Date, keyvalue, zongjie) values(?,?,?,?)";
pStmt = conn.prepareStatement(sql);
pStmt.setInt(1,item.getUid());
pStmt.setString(2,item.getDate());
pStmt.setString(3,item.getKeyvalue());
pStmt.setString(4,item.getZongjie());
iRow = pStmt.executeUpdate();

}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return iRow;
}

}

2.allMsg

package com.example.notebook.enity;

public class allMsg {
private int id;
private String uclass;
private int uid;
private String uname;
private String conDay;

public int getId() {
return id;
}

public String getUclass() {
return uclass;
}

public int getUid() {
return uid;
}

public String getUname() {
return uname;
}

public String getConDay() {
return conDay;
}

public void setId(int id) {
this.id = id;
}

public void setUclass(String uclass) {
this.uclass = uclass;
}

public void setUid(int uid) {
this.uid = uid;
}

public void setUname(String uname) {
this.uname = uname;
}

public void setConDay(String conDay) {
this.conDay = conDay;
}
}

3.Card

package com.example.notebook.enity;

public class Card {
private int id;
private int uid;
private String date;
private String keyvalue;
private String zongjie;

private String conDay;

public void setId(int id) {
this.id = id;
}

public void setUid(int uid) {
this.uid = uid;
}

public void setDate(String date) {
this.date = date;
}

public void setKeyvalue(String keyvalue) {
this.keyvalue = keyvalue;
}

public void setZongjie(String zongjie) {
this.zongjie = zongjie;
}


public int getId() {
return id;
}

public int getUid() {
return uid;
}

public String getDate() {
return date;
}

public String getKeyvalue() {
return keyvalue;
}

public String getZongjie() {
return zongjie;
}

public String getConDay() {
return conDay;
}

public void setConDay(String conDay) {
this.conDay = conDay;
}
}

4.User

package com.example.notebook.enity;

public class User {
private int uid;
private String username;
private String phone;
private String uclass;
private String password;

public User(){

}
public User(int uid, String username, String phone, String uclass, String password) {
this.uid = uid;
this.username = username;
this.phone = phone;
this.uclass = uclass;
this.password = password;
}

public int getUid() {
return uid;
}

public String getUsername() {
return username;
}

public String getPhone() {
return phone;
}

public String getUclass() {
return uclass;
}

public String getPassword() {
return password;
}

public void setUid(int uid) {
this.uid = uid;
}

public void setUsername(String username) {
this.username = username;
}

public void setPhone(String phone) {
this.phone = phone;
}

public void setUclass(String uclass) {
this.uclass = uclass;
}

public void setPassword(String password) {
this.password = password;
}
}

5.Commonutil

package com.example.notebook.util;
import android.app.AlertDialog;
import android.content.Context;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

/*
*自定义的通用工具类
*/
public class Commonutil {

/*
*显示短消息
*/
public static void showShortMsg(Context context, String msg){
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}

/*
*显示长消息
*/
public static void showLongMsg(Context context, String msg){
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}

/*
*显示消息对话框
*/
public static void showDigMsg(Context context, String msg){
new AlertDialog.Builder(context)
.setTitle("提示信息")
.setMessage(msg)
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.create().show();
}

public static String getNowDate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date());

}
}

6.MysqlHelper

package com.example.notebook.util;
import com.mysql.jdbc.Statement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/*
*Mysql数据库的连接辅助类
*/
public class MysqlHelper {
private static final String user = "root";
private static final String password = "1003392478";
private static final String dbName = "test2";
private static final String ip = "10.99.125.152";
private static final int port = 3306;
private static final String url = "jdbc:mysql://" + ip + ":" + port
+ "/" + dbName+"?useUnicode=true&characterEncoding=utf-8&useSSL=false";

public static Connection conn; //连接对象
public static Statement stmt; //命令集
public static PreparedStatement pStmt; //预编译命令集
public static ResultSet rs; //结果集

/*
*获取连接
*/
public static void getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,user,password);
}
catch (Exception e){
e.printStackTrace();
}
}

/*
*关闭连接
*/
public static void closeAll(){
try {
if(rs!=null){
rs.close();
rs = null;
}
if(stmt!=null){
stmt.close();
stmt = null;
}
if(pStmt!=null){
pStmt.close();
pStmt = null;
}
if(conn!=null){
conn.close();
conn = null;
}
} catch (Exception e){
e.printStackTrace();
}
}


}

7.SqliteHelper

package com.example.notebook.util;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.notebook.enity.Card;
import com.example.notebook.enity.User;

public class SqliteHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "temp2.db";//数据库名字
private static final int DB_VERSION = 1; //数据库版本
private static final String TABLE_NAME1 = "userinfo";//数据表名字
private static final String TABLE_NAME2 = "card";
private static final String TABLE_NAME3 = "allMsg";
private static SqliteHelper mHelper = null; //数据库帮助器
private SQLiteDatabase mRDB = null; //用来读
private SQLiteDatabase mWDB = null; //用来写

private SqliteHelper(Context context){
super(context, DB_NAME, null ,DB_VERSION);
}


//利用单例模式获取数据库帮助器的唯一实例
public static SqliteHelper getInstance(Context context){
if (mHelper == null){
mHelper = new SqliteHelper(context);
}
return mHelper;
}

//打开数据库的读连接
public SQLiteDatabase openReadLink(){
if (mRDB == null || !mRDB.isOpen()){
mRDB = mHelper.getReadableDatabase();
}
return mRDB;
}

//打开数据库的写连接
public SQLiteDatabase openWriteLink(){
if (mWDB == null || !mWDB.isOpen()){
mWDB = mHelper.getWritableDatabase();
}
return mWDB;
}

//关闭数据库连接
public void closeLink(){
if (mRDB != null && mRDB.isOpen()){
mRDB.close();
mRDB = null;
}

if (mWDB != null && mWDB.isOpen()){
mWDB.close();
mWDB = null;
}
}
//创建数据库,执行建表语句
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

/*
*储存打卡信息表
*/
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME2 + "(" +
" id INTEGER primary key AUTOINCREMENT NOT NULL, " +
" uid INTEGER NOT NULL, " +
" Date TEXT not null, " +
" keyvalue VARCHAR not null, " +
" zongjie VARCHAR not null, " +
" conDay VARCHAR not null)";
sqLiteDatabase.execSQL(sql);

}

//数据库版本更新时会执行,如用于添加新字段。。。
//当前版本DB_VERSION为1,将其更新为2后,会执行该函数
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}

public long insert(Card card){
ContentValues values = new ContentValues();
values.put("uid",card.getUid());
values.put("Date",card.getDate());
values.put("keyvalue",card.getKeyvalue());
values.put("zongjie",card.getZongjie());
values.put("conDay","111");
//执行插入,返回插入记录的行数
return mWDB.insert(TABLE_NAME2, null, values);
}
public long deleteByDate(String date){
//删除所有
//mWDB.delete(TABLE_NAME, "1=1", null);
//执行删除,返回删除记录的行数
//mWDB.delete(TABLE_NAME, "name=? and age=?", new String[]{name,age});
return mWDB.delete(TABLE_NAME2, "date!=?", new String[]{date});
}
}

8.AlarmACtivity

package com.example.notebook;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;

import com.example.notebook.util.Commonutil;

public class AlarmActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
String thing = bundle.getString("thing");
AlertDialog alert = new AlertDialog.Builder(this).create();
alert.setTitle("到时间了!");
alert.setMessage(thing);
alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
});
alert.show();
}
}

9.ClockActivity

package com.example.notebook;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;

import com.example.notebook.util.Commonutil;

import java.util.Calendar;

public class ClockActivity extends AppCompatActivity implements View.OnClickListener {

private TimePicker timePicker;
private EditText et_thing;
private String thing;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clock);
//获取时间拾取组件
timePicker = (TimePicker) findViewById(R.id.time);
timePicker.setIs24HourView(true); //设置24小时制
findViewById(R.id.btn_set).setOnClickListener(this);
et_thing = findViewById(R.id.et_thing);
}

@Override
public void onClick(View view) {
//设置闹钟
thing = et_thing.getText().toString().trim();
Intent intent = new Intent(ClockActivity.this,AlarmActivity.class);
Bundle bundle = new Bundle();
bundle.putString("thing",thing);
intent.putExtras(bundle);
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(ClockActivity.this, 0, intent,PendingIntent.FLAG_MUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(ClockActivity.this, 0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
}
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar c = Calendar.getInstance();
//获取在时间拾取器中设置的小时和分钟并给予日历对象
c.set(Calendar.HOUR_OF_DAY,timePicker.getHour());
c.set(Calendar.MINUTE,timePicker.getMinute());
c.set(Calendar.SECOND,0);
alarm.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
Commonutil.showShortMsg(ClockActivity.this,"提醒设置成功!");

}
}

10.DakaActivity

package com.example.notebook;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.notebook.Dao.UserDao;
import com.example.notebook.enity.Card;
import com.example.notebook.enity.allMsg;
import com.example.notebook.util.Commonutil;
import com.example.notebook.util.SqliteHelper;

public class DakaActivity extends AppCompatActivity implements View.OnClickListener {

private TextView pt_tv_date;
private TextView pt_tv_max;
private TextView pt_tv_conDay;
private EditText pt_et_keyvalue;
private EditText pt_et_zongjie;
private allMsg msg;
private Bundle bundle;
private int uid;
private Handler mainHandler; //主线程
private String password;
private String uclass;
private String phone;
private String username;
private SqliteHelper mHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daka);
initview();
setDate();
setconDay();
setmax();
findViewById(R.id.pt_btn_card).setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.pt_btn_card:
doputch();
break;
}

}
public void initview(){
pt_tv_date = findViewById(R.id.pt_tv_date);
pt_tv_max = findViewById(R.id.pt_tv_max);
pt_tv_conDay = findViewById(R.id.pt_tv_conDay);
pt_et_keyvalue = findViewById(R.id.pt_et_keyvalue);
pt_et_zongjie = findViewById(R.id.pt_et_zongjie);
mainHandler = new Handler(getMainLooper()); //获取主线程
bundle = getIntent().getExtras();
uid = bundle.getInt("uid");
username = bundle.getString("username");
phone = bundle.getString("phone");
uclass = bundle.getString("class");
password = bundle.getString("password");
msg = new allMsg();
}

public void setDate(){
String date = Commonutil.getNowDate();
pt_tv_date.setText(date);
}
public void setconDay(){
new Thread(new Runnable() {
@Override
public void run() {
mainHandler.post(new Runnable() {
int i = UserDao.getConDayByUid(Integer.toString(uid));
@Override
public void run() {
if(i == 0){
pt_tv_conDay.setText("还没有打过卡!");
}
else{
String desc = String.format("已经打卡:%d天!",i);
pt_tv_conDay.setText(desc);
}
}
});

}
}).start();
}
public void setmax(){
new Thread(new Runnable() {
@Override
public void run() {
mainHandler.post(new Runnable() {
int i = UserDao.getMaxByUid(Integer.toString(uid));
@Override
public void run() {
if(i == 0){
pt_tv_max.setText("还没有打过卡!");
}
else{
String desc = String.format("最长连续打卡:%d天!",i);
pt_tv_max.setText(desc);
}
}
});

}
}).start();
}
public void doputch(){
String date = pt_tv_date.getText().toString().trim();
String keyvalue = pt_et_keyvalue.getText().toString().trim();
String zongjie = pt_et_zongjie.getText().toString().trim();
Card card = new Card();
card.setDate(date);
card.setUid(uid);
card.setKeyvalue(keyvalue);
card.setZongjie(zongjie);

new Thread(new Runnable() {
@Override
public void run() {
int iRow = 0;
String nowDate = Commonutil.getNowDate();
mHelper.deleteByDate(nowDate);
boolean flag = UserDao.CompareDate(Integer.toString(uid));
if(!flag){
iRow = UserDao.addCard(card);
mHelper.insert(card);
}
int finalIRow = iRow;
mainHandler.post(new Runnable() {
@Override
public void run() {
if(flag){
Commonutil.showShortMsg(DakaActivity.this,"今天已经打过卡了");
Intent intent = new Intent(DakaActivity.this, XInxiActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("uid", uid); //键值对
bundle.putString("username",username);
bundle.putString("phone",phone);
bundle.putString("class",uclass);
bundle.putString("password",password);
intent.putExtras(bundle);
startActivity(intent);
}
else{
if(finalIRow == 0){
Commonutil.showDigMsg(DakaActivity.this,"打卡失败");
}
else {
Commonutil.showShortMsg(DakaActivity.this,"打卡成功");
Intent intent = new Intent(DakaActivity.this, XInxiActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("uid", uid); //键值对
bundle.putString("username",username);
bundle.putString("phone",phone);
bundle.putString("class",uclass);
bundle.putString("password",password);
intent.putExtras(bundle);
startActivity(intent);
}
}
}
});
}
}).start();
}

@Override
protected void onStart() {
super.onStart();
//获取数据库帮助器的实例
mHelper = SqliteHelper.getInstance(this);
//打开数据库帮助器的读写连接
mHelper.openWriteLink();
mHelper.openReadLink();
}

@Override
protected void onStop() {
super.onStop();
//关闭数据库连接
mHelper.closeLink();
}

}

11.LoginActivity

package com.example.notebook;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.example.notebook.Dao.UserDao;
import com.example.notebook.enity.User;
import com.example.notebook.util.Commonutil;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

private EditText et_uid;
private EditText et_password;
private Handler mainHandler; //主线程

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

mainHandler = new Handler(getMainLooper()); //获取主线程
et_uid = findViewById(R.id.et_uid);
et_password = findViewById(R.id.et_password);

findViewById(R.id.btn_login).setOnClickListener(this);
findViewById(R.id.btn_register).setOnClickListener(this);
}

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_login:
dologin();

break;
case R.id.btn_register:
Intent intent = new Intent(this, RegisterActivity.class);
startActivity(intent);
break;
}
}
private void dologin(){
final String uid = et_uid.getText().toString().trim();
final String upass = et_password.getText().toString().trim();

if(TextUtils.isEmpty(uid)){
Commonutil.showShortMsg(this,"学号为空!");
et_uid.requestFocus();
}
else if (TextUtils.isEmpty(upass)){
Commonutil.showShortMsg(this,"密码为空!");
et_password.requestFocus();
}
else {
new Thread(new Runnable() {
@Override
public void run() {
final User item = UserDao.getUserByUidAndUpass(uid,upass);
mainHandler.post(new Runnable() {

@Override
public void run() {
if(item == null){
Commonutil.showDigMsg(LoginActivity.this,"学号或密码错误!");
}
else {
Commonutil.showShortMsg(LoginActivity.this,"登录成功!");
Intent intent = new Intent(LoginActivity.this, XInxiActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("uid", item.getUid()); //键值对
bundle.putString("username",item.getUsername());
bundle.putString("phone",item.getPhone());
bundle.putString("class",item.getUclass());
bundle.putString("password",item.getPassword());
intent.putExtras(bundle);
startActivity(intent);
}
}
});

}
}).start();
}

}
}

12.MainActivity

package com.example.notebook;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;

import com.example.notebook.util.SqliteHelper;
import com.example.notebook.util.Commonutil;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {


private SqliteHelper mHelper;
private String mDatabaseName;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_enter).setOnClickListener(this);
mDatabaseName = getFilesDir() + "/test.db";


}


@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_enter:
//创建或者打开数据库,如果数据库不存在就创建,如果存在的话就打开数据库
SQLiteDatabase db = openOrCreateDatabase(mDatabaseName, Context.MODE_PRIVATE,null);
Commonutil.showLongMsg(this,"进入软件!");
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
break;
}
}

@Override
protected void onStart() {
super.onStart();
//获取数据库帮助器的实例
mHelper = SqliteHelper.getInstance(this);
//打开数据库帮助器的读写连接
mHelper.openWriteLink();
mHelper.openReadLink();
}

@Override
protected void onStop() {
super.onStop();
//关闭数据库连接
//mHelper.closeLink();
}
}

13.RegisterActivity

package com.example.notebook;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.notebook.Dao.UserDao;
import com.example.notebook.enity.User;
import com.example.notebook.util.Commonutil;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

private String[] starArray = {"信2105-1班", "信2105-2班", "信2105-3班"};
private String uclass;
private EditText et_reusername;
private EditText et_repassword;
private EditText et_phone;
private User user;

private Handler mainHandler; //主线程

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
init();
findViewById(R.id.btn_register).setOnClickListener(this);
}

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_register:
doregister();
}
}

private void init() {
//声明一个下拉列表的数组适配器
ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this, R.layout.item_select, starArray);
//设置数组适配器的布局样式
starAdapter.setDropDownViewResource(R.layout.item_dropdown);
//从布局文件中获取名叫sp_dialog的下拉框
Spinner sp = findViewById(R.id.spinner);
//设置下拉框的标题,不设置就没有难看的标题了
sp.setPrompt("请选择班级");
//设置下拉框的数组适配器
sp.setAdapter(starAdapter);
//设置下拉框默认的显示第一项
sp.setSelection(0);
//给下拉框设置选择监听器,一旦用户选中某一项,就触发监听器的onItemSelected方法
sp.setOnItemSelectedListener(new MySelectedListener());

mainHandler = new Handler(getMainLooper()); //获取主线程

et_reusername = findViewById(R.id.et_Reusername);
et_repassword = findViewById(R.id.et_Repassword);
et_phone = findViewById(R.id.et_phone);
}


class MySelectedListener implements AdapterView.OnItemSelectedListener {

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
uclass = starArray[i];
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
}

private void doregister(){
String reusername = et_reusername.getText().toString().trim();
String repassword = et_repassword.getText().toString().trim();
String phone = et_phone.getText().toString().trim();
user = new User();
user.setUsername(reusername);
user.setPhone(phone);
user.setUclass(uclass);
user.setPassword(repassword);
new Thread(new Runnable() {
@Override
public void run() {
int iRow = UserDao.addUser(user);
mainHandler.post(new Runnable() {

@Override
public void run() {
if(iRow == 0){
Commonutil.showShortMsg(RegisterActivity.this,"注册失败");
}
else {
Commonutil.showShortMsg(RegisterActivity.this,"注册成功");
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
}
}
});

}
}).start();
}


}

14.XinxiActivity

package com.example.notebook;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class XInxiActivity extends AppCompatActivity implements View.OnClickListener {

private TextView tv_uid;
private TextView tv_name;
private TextView tv_prclass;
private TextView tv_phone;
private String username;
private String phone;
private String uclass;
private String password;
private int uid;
private Bundle bundle;
private Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xinxi);
showMsg();

findViewById(R.id.btn_card).setOnClickListener(this);
findViewById(R.id.btn_clock).setOnClickListener(this);

}

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_card:
intent = new Intent(XInxiActivity.this, DakaActivity.class);
Bundle bundle1 = new Bundle();
bundle1.putInt("uid", uid); //键值对
bundle1.putString("username",username);
bundle1.putString("phone",phone);
bundle1.putString("class",uclass);
bundle1.putString("password",password);
intent.putExtras(bundle1);
startActivity(intent);
break;
case R.id.btn_clock:
intent = new Intent(XInxiActivity.this, ClockActivity.class);
Bundle bundle2 = new Bundle();
bundle2.putInt("uid", uid); //键值对
bundle2.putString("username",username);
bundle2.putString("phone",phone);
bundle2.putString("class",uclass);
bundle2.putString("password",password);
intent.putExtras(bundle2);
startActivity(intent);

}

}

public void showMsg(){
bundle = getIntent().getExtras();
uid = bundle.getInt("uid");
username = bundle.getString("username");
phone = bundle.getString("phone");
uclass = bundle.getString("class");
password = bundle.getString("password");

tv_uid = findViewById(R.id.tv_uid);
tv_name = findViewById(R.id.tv_name);
tv_prclass = findViewById(R.id.tv_prclass);
tv_phone = findViewById(R.id.tv_phone);

tv_uid.setText("" + uid);
tv_name.setText(username);
tv_prclass.setText(uclass);
tv_phone.setText(phone);
}

}

15.activity_alarm

<?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=".AlarmActivity">

</androidx.constraintlayout.widget.ConstraintLayout>

16.activity_clock

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">

<TimePicker
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<EditText
android:id="@+id/et_thing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入待提醒的事宜"/>

<Button
android:id="@+id/btn_set"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置提醒" />

</LinearLayout>

17.activity_daka

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="日 期:"
android:textStyle="bold"
android:textSize="20dp"
android:textColor="@color/purple_200"/>

<TextView
android:id="@+id/pt_tv_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textStyle="bold"
android:textSize="20dp"
android:textColor="@color/purple_200"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关键字:"
android:textStyle="bold"
android:textSize="20dp"
android:textColor="@color/purple_200"/>

<EditText
android:id="@+id/pt_et_keyvalue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入每日关键字"
android:textSize="20dp" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="总 结:"
android:textStyle="bold"
android:textSize="20dp"
android:textColor="@color/purple_200"/>

<EditText
android:id="@+id/pt_et_zongjie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入每日总结"
android:textSize="20dp" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已坚持:"
android:textStyle="bold"
android:textSize="20dp"
android:textColor="@color/purple_200"/>

<TextView
android:id="@+id/pt_tv_conDay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="连续最长坚持:"
android:textStyle="bold"
android:textSize="20dp"
android:textColor="@color/purple_200"/>

<TextView
android:id="@+id/pt_tv_max"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp" />
</LinearLayout>

<Button
android:id="@+id/pt_btn_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打卡"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginTop="50dp"/>

</LinearLayout>

18.activity_login

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="每日打卡"
android:gravity="center"
android:textSize="30dp"
android:textColor="@color/purple_200"
android:textStyle="bold"
android:layout_marginTop="50dp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学 号:"
android:textStyle="bold"
android:textColor="@color/black"
android:layout_marginBottom="50dp"
android:textSize="20dp"/>

<EditText
android:id="@+id/et_uid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入学号"
android:textSize="20sp"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:textStyle="bold"
android:textColor="@color/black"
android:textSize="20dp"/>

<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="请输入密码"
android:textSize="20sp"/>
</LinearLayout>

<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginTop="50dp"/>

<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击注册"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginTop="50dp"/>


</LinearLayout>

19.activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="每日打卡"
android:gravity="center"
android:textSize="30dp"
android:textColor="@color/purple_200"
android:textStyle="bold"
android:layout_marginTop="50dp"/>



<Button
android:id="@+id/btn_enter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击进入"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginTop="50dp"/>


</LinearLayout>

20.activity_register

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册界面"
android:gravity="center"
android:textSize="30dp"
android:textColor="@color/purple_200"
android:textStyle="bold"
android:layout_marginTop="50dp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓 名:"
android:textStyle="bold"
android:textColor="@color/black"
android:textSize="20dp"/>

<EditText
android:id="@+id/et_Reusername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名"
android:textSize="20sp"/>
</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机号:"
android:textStyle="bold"
android:textColor="@color/black"
android:textSize="20dp"/>

<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入手机号"
android:textSize="20sp"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:textStyle="bold"
android:textColor="@color/black"
android:textSize="20dp"/>

<EditText
android:id="@+id/et_Repassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:textSize="20sp"/>
</LinearLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/tv_class"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="班 级:"
android:textStyle="bold"
android:textColor="@color/black"
android:textSize="20dp"/>

<Spinner
android:id="@+id/spinner"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/tv_class"
android:background="#E5E5E5"
android:dropDownVerticalOffset="45dp"
android:spinnerMode="dropdown" />
</RelativeLayout>

<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginTop="20dp"/>

</LinearLayout>

21.activity_xinxi

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学号:"
android:textStyle="bold"
android:textSize="20dp"
android:textColor="@color/purple_200"/>

<TextView
android:id="@+id/tv_uid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="bold"
android:textSize="20dp"
android:textColor="@color/purple_200"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textStyle="bold"
android:textSize="20dp"
android:textColor="@color/purple_200"/>

<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="bold"
android:textSize="20dp"
android:textColor="@color/purple_200"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="班级:"
android:textStyle="bold"
android:textSize="20dp"
android:textColor="@color/purple_200"/>

<TextView
android:id="@+id/tv_prclass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="bold"
android:textSize="20dp"
android:textColor="@color/purple_200"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机号:"
android:textStyle="bold"
android:textSize="20dp"
android:textColor="@color/purple_200"/>

<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="bold"
android:textSize="20dp"
android:textColor="@color/purple_200"/>
</LinearLayout>

<Button
android:id="@+id/btn_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="每日打卡"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginTop="50dp"/>

<Button
android:id="@+id/btn_clock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置提醒"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginTop="50dp"/>


</LinearLayout>

 

posted @   Joranger  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示