安卓 员工管理系统app

员工管理系统、

开发工具:Android Studio
数据库: sqlite

数据库

部分源码
manifest.xml配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.company">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SearchActivity"></activity>
        <activity android:name=".GuanliActivity" />
        <activity android:name=".GloginActivity" />
        <activity android:name=".RegisterActivity" />
        <activity android:name=".IntroductionActivity" />
        <activity android:name=".NewsActivity" />
        <activity android:name=".FirstActivity" />

        <activity android:name=".LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

员工界面

package com.example.company;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class FirstActivity extends AppCompatActivity {
private Cursor cursor;
@Override
public void onDestroy(){
super.onDestroy();
cursor.close();
}
//定义共享优先数据及基础字段  用于签到功能实现
private String MY_RMBCost ="MY_RMBCost";
private String TodayTime ="TodayTime";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
//--------------------------------------------------------------登录
Button btnlongin = findViewById(R.id.login);
btnlongin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this,LoginActivity.class);
startActivity(intent);
}
});
//-------------------------------------------------------------签到
final Button bt_qiandao = (Button)findViewById(R.id.bt_qiandao);
final TextView tv_time = (TextView)findViewById(R.id.tv_qiandaotime);
SharedPreferences my_rmb_data = getSharedPreferences(MY_RMBCost, 0);//读取共享数据
Time t = new Time();
t.setToNow();
int lastmonth = t.month + 1 ;
final String str =  t.year + "年" + lastmonth + "月" + t.monthDay + "日";
final String nowtime =my_rmb_data.getString(TodayTime, "").toString();
if(nowtime.equals(str)==true)
{
tv_time.setText("日期:"+ nowtime +"已签到!");
bt_qiandao.setBackgroundResource(R.drawable.yqd);
bt_qiandao.setText("签到");
}
else
{
tv_time.setText("日期:"+ str);
bt_qiandao.setBackgroundResource(R.drawable.qd);
}
//签到功能
bt_qiandao.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences my_rmb_data = getSharedPreferences(MY_RMBCost, 0);
if(my_rmb_data.getString(TodayTime, "").toString().equals(str)==true)
{
Toast.makeText(FirstActivity.this , "今日已签到!", Toast.LENGTH_SHORT).show();
}
else
{
my_rmb_data.edit()
.putString(TodayTime, str)
.commit();
tv_time.setText("日期:"+ str +"已签到!");
bt_qiandao.setBackgroundResource(R.drawable.yqd);
bt_qiandao.setText("签到");
Toast.makeText( FirstActivity.this , "签到成功!", Toast.LENGTH_SHORT).show();
}
}
});
//-------------------------------------每日资讯
//ArrayAdapter<News> listAdapter= new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,News.news);
ListView listnews = findViewById(R.id.list_news);
//listnews.setAdapter(listAdapter);
//设置游标适配器
SQLiteOpenHelper starbuzzDatabaseHelper=new CompanyDatabaseHelper(this);
//获得数据库引用
try {
SQLiteDatabase db =starbuzzDatabaseHelper.getReadableDatabase();
cursor = db.query("NEWS",
new String[]{"_id","TITLE"},
null,null,null,null,null);
SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
cursor,
new String[]{"TITLE"},
new int[]{android.R.id.text1},
0);
listnews.setAdapter(listAdapter);
}catch (SQLException e){
Log.e("sqlite",e.getMessage());
Toast toast=Toast.makeText(this,"Database unavaiable",Toast.LENGTH_SHORT);
toast.show();
}
//指定监听器 相应选项单击
AdapterView.OnItemClickListener itemClickListener=new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(FirstActivity.this,NewsActivity.class);
intent.putExtra(NewsActivity.EXTRA_DRINKID,(int)id);
startActivity(intent);
}
};
listnews.setOnItemClickListener(itemClickListener);
//-------------------------------------------------公司简介
Button btnintroduction = findViewById(R.id.introduction);
btnintroduction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this,IntroductionActivity.class);
startActivity(intent);
}
});
}
}

登录注册

package com.example.company;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_login);
//
//        Button mbtnlogin = findViewById(R.id.btn_login);
//        mbtnlogin.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
//                Intent intent = new Intent(LoginActivity.this,FirstActivity.class);
//                startActivity(intent);
//            }
//        });
//
//        Button btnback = findViewById(R.id.btn_back);
//        btnback.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(LoginActivity.this,FirstActivity.class);
//                startActivity(intent);
//            }
//        });
//    }
private CompanyDatabaseHelper mDBOpenHelper;
private EditText mEtLoginactivityUsername;
private EditText mEtLoginactivityPassword;
private Button mBtLonginactivittRegister;
private Button mBtLoginactivityLogin;
private Button mBtLoginactivityGl;
private String rname;
private String rpassword;
/**
* 创建 Activity 时先来重写 onCreate() 方法
* 保存实例状态
* super.onCreate(savedInstanceState);
* 设置视图内容的配置文件
* setContentView(R.layout.activity_login);
* 上面这行代码真正实现了把视图层 View 也就是 layout 的内容放到 Activity 中进行显示
* 初始化视图中的控件对象 initView()
* 实例化 DBOpenHelper,待会进行登录验证的时候要用来进行数据查询
* mDBOpenHelper = new DBOpenHelper(this);
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initView();
rpassword();
mDBOpenHelper = new CompanyDatabaseHelper(this);
}
//记住密码
private void rpassword(){
if(rname!=null && rpassword!=null)
{
mEtLoginactivityUsername.setText(rname);
mEtLoginactivityPassword.setText(rpassword);
}
}
/**
* onCreae()中大的布局已经摆放好了,接下来就该把layout里的东西
* 声明、实例化对象然后有行为的赋予其行为
* 这样就可以把视图层View也就是layout 与 控制层 Java 结合起来了
*/
private void initView() {
// 初始化控件
mBtLoginactivityLogin = findViewById(R.id.bt_loginactivity_login);
mBtLoginactivityGl = findViewById(R.id.bt_loginactivityGl);
mBtLonginactivittRegister = findViewById(R.id.bt_loginactivity_register);
mEtLoginactivityUsername = findViewById(R.id.et_loginactivity_username);
mEtLoginactivityPassword = findViewById(R.id.et_loginactivity_password);
// 设置点击事件监听器
mBtLoginactivityLogin.setOnClickListener(this);
mBtLonginactivittRegister.setOnClickListener(this);
mBtLoginactivityGl.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
// 跳转到注册界面
case R.id.bt_loginactivity_register:
startActivity(new Intent(this, RegisterActivity.class));
finish();
break;
case R.id.bt_loginactivityGl:
startActivity(new Intent(this,GloginActivity.class));
finish();
break;
/**
* 登录验证:
*
* 从EditText的对象上获取文本编辑框输入的数据,并把左右两边的空格去掉
*  String name = mEtLoginactivityUsername.getText().toString().trim();
*  String password = mEtLoginactivityPassword.getText().toString().trim();
*  进行匹配验证,先判断一下用户名密码是否为空,
*  if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password))
*  再进而for循环判断是否与数据库中的数据相匹配
*  if (name.equals(user.getName()) && password.equals(user.getPassword()))
*  一旦匹配,立即将match = true;break;
*  否则 一直匹配到结束 match = false;
*
*  登录成功之后,进行页面跳转:
*
*  Intent intent = new Intent(this, MainActivity.class);
*  startActivity(intent);
*  finish();//销毁此Activity
*/
case R.id.bt_loginactivity_login:
String name = mEtLoginactivityUsername.getText().toString().trim();
String password = mEtLoginactivityPassword.getText().toString().trim();
rname = name;
rpassword = password;
if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)) {
ArrayList<User> data = mDBOpenHelper.getAllData();
boolean match = false;
for (int i = 0; i < data.size(); i++) {
User user = data.get(i);
if (name.equals(user.getName()) && password.equals(user.getPassword())) {
match = true;
break;
} else {
match = false;
}
}
if (match) {
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, FirstActivity.class);
startActivity(intent);
finish();//销毁此Activity
} else {
Toast.makeText(this, "用户名或密码不正确,请重新输入", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "请输入你的用户名或密码", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
package com.example.company;
import android.content.Intent;
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 androidx.appcompat.app.AppCompatActivity;
/**
*/
/**
* 此类 implements View.OnClickListener 之后,
* 就可以把onClick事件写到onCreate()方法之外
* 这样,onCreate()方法中的代码就不会显得很冗余
*/
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
private CompanyDatabaseHelper mDBOpenHelper;
private Button mBtRegisteractivityRegister;
private EditText mEtRegisteractivityUsername;
private EditText mEtRegisteractivityPassword1;
private EditText mEtRegisteractivityPassword2;
private Button mBtRegisteractivityBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
initView();
mDBOpenHelper = new CompanyDatabaseHelper(this);
}
private void initView(){
mBtRegisteractivityRegister = findViewById(R.id.bt_registeractivity_register);
mBtRegisteractivityBack = findViewById(R.id.bt_registeractivity_back);
mEtRegisteractivityUsername = findViewById(R.id.et_registeractivity_username);
mEtRegisteractivityPassword1 = findViewById(R.id.et_registeractivity_password1);
mEtRegisteractivityPassword2 = findViewById(R.id.et_registeractivity_password2);
/**
* 注册页面能点击的就三个地方
* top处返回箭头、刷新验证码图片、注册按钮
*/
mBtRegisteractivityBack.setOnClickListener(this);
mBtRegisteractivityRegister.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_registeractivity_back: //返回登录页面
Intent intent1 = new Intent(this, LoginActivity.class);
startActivity(intent1);
finish();
break;
case R.id.bt_registeractivity_register:    //注册按钮
//获取用户输入的用户名、密码、验证码
String username = mEtRegisteractivityUsername.getText().toString().trim();
String password1 = mEtRegisteractivityPassword1.getText().toString().trim();
String password2 = mEtRegisteractivityPassword2.getText().toString().trim();
//注册验证
if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password1) && !TextUtils.isEmpty(password2) ) {
if (password1.equals(password2)) {
//将用户名和密码加入到数据库中
mDBOpenHelper.login_add(username, password1);
Intent intent2 = new Intent(this, FirstActivity.class);
startActivity(intent2);
finish();
Toast.makeText(this,  "验证通过,注册成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "密码不一致,注册失败", Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(this, "未完善信息,注册失败", Toast.LENGTH_SHORT).show();
}
break;
}
}
}

管理员

package com.example.company;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
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;
public class GuanliActivity extends AppCompatActivity implements View.OnClickListener {
private Cursor cursor;
private CompanyDatabaseHelper mDBOpenHelper;
private Button mbtadd;
private Button mbtdelete;
private  Button mbtupdate;
private Button mbtsearch;
private Button mbtback;
private EditText medtname;
private EditText medtsex;
private EditText medttel;
private EditText medtposition;
private EditText medttime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guanli);
initView();
mDBOpenHelper = new CompanyDatabaseHelper(this);
}
private void initView(){
mbtadd = findViewById(R.id.bt_add);
mbtdelete = findViewById(R.id.bt_delete);
mbtupdate = findViewById(R.id.bt_update);
mbtsearch = findViewById(R.id.bt_search);
mbtback = findViewById(R.id.bt_guanliback);
medtname = findViewById(R.id.edt_name);
medtsex = findViewById(R.id.edt_sex);
medttel = findViewById(R.id.edt_tel);
medtposition = findViewById(R.id.edt_position);
medttime = findViewById(R.id.edt_time);
mbtadd.setOnClickListener(this);
mbtdelete.setOnClickListener(this);
mbtupdate.setOnClickListener(this);
mbtsearch.setOnClickListener(this);
mbtback.setOnClickListener(this);
}
public void onClick(View view){
switch (view.getId()){
case R.id.bt_add:
//                int mid =Integer.parseInt (medtid.getText().toString());
String name = medtname.getText().toString().trim();
String sex = medtsex.getText().toString().trim();
String tel = medttel.getText().toString().trim();
String position = medtposition.getText().toString().trim();
String time = medttime.getText().toString().trim();
mDBOpenHelper.yuangong_add(name,sex,tel,position,time);
Toast.makeText(this,  "添加成功", Toast.LENGTH_SHORT).show();
medtname.setText("");
medtsex.setText("");
medttel.setText("");
medtposition.setText("");
medttime.setText("");
break;
case R.id.bt_delete:
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("删除员工信息")
.setMessage("是否删除")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String name = medtname.getText().toString().trim();
String position = medtposition.getText().toString().trim();
mDBOpenHelper.yuangong_delete(name);
medtname.setText("");
medtsex.setText("");
medttel.setText("");
medtposition.setText("");
medttime.setText("");
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
return;
}
}).create();
alertDialog.show();
break;
case R.id.bt_update:
String uname = medtname.getText().toString().trim();
String utel = medttel.getText().toString().trim();
String uposition = medtposition.getText().toString().trim();
mDBOpenHelper.yuangong_updata(uname,uposition,utel);
Toast.makeText(this,"更新成功",Toast.LENGTH_SHORT).show();
medtname.setText("");
medtsex.setText("");
medttel.setText("");
medtposition.setText("");
medttime.setText("");
break;
case R.id.bt_search:
startActivity(new Intent(GuanliActivity.this, SearchActivity.class));
finish();//销毁此Activity
break;
case R.id.bt_guanliback:
startActivity(new Intent(GuanliActivity.this, GloginActivity.class));
finish();//销毁此Activity
break;
}
}
}

数据库

package com.example.company;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
public class CompanyDatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME="company.db";
private static final int DB_VER=1;
private SQLiteDatabase db;
public CompanyDatabaseHelper(Context context){
super(context,DB_NAME,null,DB_VER);
db = getReadableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
//建每日讯息表
db.execSQL("CREATE TABLE NEWS(_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"TITLE TEXT," +
"IMAGE_RESOURCE_ID INTEGER," +
"CONTENT TEXT);");
//建立登录表
db.execSQL("CREATE TABLE USER( _id INTEGER PRIMARY KEY AUTOINCREMENT," +
"name TEXT," +
"password TEXT);");
//建立管理员登录表
db.execSQL("CREATE TABLE GUSER( _id INTEGER PRIMARY KEY AUTOINCREMENT," +
"gname TEXT," +
"password TEXT);");
db.execSQL("INSERT INTO guser (gname,password) VALUES('zzz','123')");
//建立用户信息表
db.execSQL("CREATE TABLE YUANGONG( _id INTEGER PRIMARY KEY AUTOINCREMENT," +
"name TEXT," +
"sex TEXT,"+
"tel TEXT,"+
"position TEXT,"+
"TIME DATE);");
//插入每日讯息
insertNews(db,"通知1",R.drawable.news01,"aaaaaaaaaaaaaaaaaaaaaaaa");
insertNews(db,"通知2",R.drawable.news02,"bbbbbbbbbbbbbbbbbbbbbbbbbb");
}
private static void insertNews(SQLiteDatabase db,String title,int resourceId,String content){
ContentValues newsValues = new ContentValues();
newsValues.put("TITLE",title);
newsValues.put("IMAGE_RESOURCE_ID",resourceId);
newsValues.put("CONTENT",content);
long result = db.insert("NEWS",null,newsValues);
//日志插入结果
Log.d("sqlite","insert"+title+"_id"+result);
}
//登录表的相关操作
public void login_add(String name,String password){
db.execSQL("INSERT INTO user (name,password) VALUES(?,?)",new Object[]{name,password});
}
public void login_delete(String name,String password){
db.execSQL("DELETE FROM user WHERE name = AND password ="+name+password);
}
public void login_updata(String password){
db.execSQL("UPDATE user SET password = ?",new Object[]{password});
}
//管理员登录表的操作
public void glogin_add(String name,String password){
db.execSQL("INSERT INTO user (name,password) VALUES(?,?)",new Object[]{name,password});
}
public void glogin_delete(String name,String password){
db.execSQL("DELETE FROM user WHERE name = AND password ="+name+password);
}
public void glogin_updata(String password){
db.execSQL("UPDATE user SET password = ?",new Object[]{password});
}
public ArrayList<User> getAllData2(){
ArrayList<User> list = new ArrayList<User>();
Cursor cursor = db.query("guser",null,null,null,null,null,"gname DESC");
while(cursor.moveToNext()){
String gname = cursor.getString(cursor.getColumnIndex("gname"));
String password = cursor.getString(cursor.getColumnIndex("password"));
list.add(new User(gname,password));
}
cursor.close();
return list;
}
public ArrayList<User> getAllData(){
ArrayList<User> list = new ArrayList<User>();
Cursor cursor = db.query("user",null,null,null,null,null,"name DESC");
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String password = cursor.getString(cursor.getColumnIndex("password"));
list.add(new User(name,password));
}
cursor.close();
return list;
}
//员工表的相关操作
public void yuangong_add(String name,String sex,String tel,String position,String time){
db.execSQL("INSERT INTO yuangong (name,sex,tel,position,time) VALUES(?,?,?,?,?)",new Object[]{name,sex,tel,position,time});
}
public void yuangong_delete(String name){
db.execSQL("DELETE FROM yuangong WHERE name = ? ", new Object[]{name});
}
public void yuangong_updata(String name,String tel,String position){
db.execSQL("update yuangong set tel=?,position=? where name=?", new Object[]{tel,position,name});
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
}
}

本文地址:https://blog.csdn.net/Kiven_zhou/article/details/107212214

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐