APP需要实现如下功能:
a.普通用户:
(1)用户管理。包括用户注册,用户登录、用户信息管理。
(2)显示联系人。用户登录成功后,可以显示所有该用户的联系人。
(3)添加联系人。用户登录成功后,可增加和用户联系人。
(4)查找联系人。用户登录成功后,可以精确查找指定联系人或按类别查找联系人。
(5)删除指定条件联系人。用户登录成功后,可以删除用户个人相关的联系人。
(6)修改联系人信息。用户登录成功后,可以修改用户相关的联系人信息。
b.管理员用户:
对系统的用户进行管理。包括重置密码、注销用户等功能。
源码在资源处可下载
https://download.csdn.net/download/2302_79553009/89665188
package com.example.addressbook;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.addressbook.Util.ToastUtil;
public class MainActivity extends AppCompatActivity {
private Button login,reset;
private TextView register;
private EditText edName,edPassword;
private MySQLiteOpenHelper mySQLiteOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
init();
}
public void init(){
login = findViewById(R.id.login);
register = findViewById(R.id.register);
edName = findViewById(R.id.edName);
edPassword = findViewById(R.id.edPassword);
reset = findViewById(R.id.reset);
}
public void login(View view) {
String name = edName.getText().toString().trim();
String password = edPassword.getText().toString().trim();
int login = mySQLiteOpenHelper.U_login(name,password);
if(login == 1){
ToastUtil.toastShort(this,"登录成功!欢迎管理员!");
Intent intent = new Intent(this,ManagerAbActivity.class);
startActivity(intent);
}else if(login == 2){
ToastUtil.toastShort(this,"登录成功!欢迎会员!");
Intent intent = new Intent(this,UserAbActivity.class);
startActivity(intent);
}else{
ToastUtil.toastShort(this,"登录失败");
}
}
public void register(View view) {
Intent intent = new Intent(this, RegisterActivity.class);
startActivity(intent);
}
public void reset(View view) {
edName.setText("");
edPassword.setText("");
}
}
package com.example.addressbook;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.addressbook.Util.ToastUtil;
import com.example.addressbook.bean.User;
public class RegisterActivity extends AppCompatActivity {
private EditText name1,password1;
private TextView l;
private TextView register;
private Button reset;
private MySQLiteOpenHelper mySQLiteOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
init();
}
public void init(){
name1 = findViewById(R.id.edName);
password1 = findViewById(R.id.edPassword);
register = findViewById(R.id.register);
l = findViewById(R.id.L);
reset = findViewById(R.id.reset);
}
public void register(View view) {
String name = name1.getText().toString().trim();
String password = password1.getText().toString().trim();
User u = new User(name,password);
long r = mySQLiteOpenHelper.U_register(u);
if(r!=-1){
ToastUtil.toastShort(this,"注册成功");
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}else{
ToastUtil.toastShort(this,"注册失败");
}
}
public void Log(View view) {
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
public void reset(View view) {
name1.setText("");
password1.setText("");
}
}
package com.example.addressbook;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import com.example.addressbook.adapter.PeopleAdapter;
import com.example.addressbook.bean.People;
import com.example.addressbook.dao.PeopleDao;
import java.util.Comparator;
import java.util.List;
public class UserAbActivity extends AppCompatActivity {
private MySQLiteOpenHelper mySQLiteOpenHelper;
private EditText search;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_ab);
Toolbar toolbar = this.findViewById(R.id.toolbar);
mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
//加载列表
List result = mySQLiteOpenHelper.getAllPeople();
ListView listView = findViewById(R.id.book);
if(result.size()==0){
listView.setAdapter(null);//无显示
}else{
//需要对list按字母进行排序
result.sort(new Comparator() {
@Override
public int compare(People people, People t1) {
return people.getBeginLetter().compareTo(t1.getBeginLetter());
}
});
PeopleAdapter peopleAdapter = new PeopleAdapter(UserAbActivity.this,result);
listView.setAdapter(peopleAdapter);
}
init();
//搜索功能
search.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
listView.setAdapter(null);
String title = search.getText().toString();
List temp = null;
if(title.isEmpty()){
temp = mySQLiteOpenHelper.getAllPeople();
}else{
temp = mySQLiteOpenHelper.getAllPeople(title);
}
temp.sort(new Comparator() {
@Override
public int compare(People people, People t1) {
return people.getBeginLetter().compareTo(t1.getBeginLetter());
}
});
PeopleAdapter peopleAdapter = new PeopleAdapter(UserAbActivity.this,temp);
listView.setAdapter(peopleAdapter);
return false;
}
});
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
listView.setAdapter(null);
String title = search.getText().toString();
List temp = null;
if(title.isEmpty()){
temp = mySQLiteOpenHelper.getAllPeople();
}else{
temp = mySQLiteOpenHelper.getAllPeople(title);
}
temp.sort(new Comparator() {
@Override
public int compare(People people, People t1) {
return people.getBeginLetter().compareTo(t1.getBeginLetter());
}
});
PeopleAdapter peopleAdapter = new PeopleAdapter(UserAbActivity.this,temp);
listView.setAdapter(peopleAdapter);
}
});
}
public void init(){
search = findViewById(R.id.search);
}
public void details(View view) {
// Intent intent = new Intent(UserAbActivity.this,DetailsActivity.class);
// intent.putExtra("id",people.getId());
// startActivity(intent);
}
public void addData(View view) {
Intent intent = new Intent(UserAbActivity.this,AddActivity.class);
startActivity(intent);
}
}
book_style.xml
package com.example.addressbook;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.addressbook.Util.ToastUtil;
import com.example.addressbook.bean.People;
public class DetailsActivity extends AppCompatActivity {
private ImageView head;
private TextView detail_name;
private TextView detail_number;
private TextView detail_remark;
private TextView detail_sex;
private MySQLiteOpenHelper mySQLiteOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
init();
//详情信息同步
String id = getIntent().getStringExtra("id");
People people = mySQLiteOpenHelper.getOnePeople(id);
//根据性别设置头像
if(people.getSex().equals("男")){
head.setImageResource(R.drawable.details_man);
}else{
head.setImageResource(R.drawable.details_woman);
}
//设置名字
detail_name.setText(people.getName());
detail_number.setText(people.getPhoneNumber());
detail_sex.setText(people.getSex());
detail_remark.setText(people.getRemark());
}
private void init(){
head = findViewById(R.id.head);
detail_name = findViewById(R.id.detail_name);
detail_number = findViewById(R.id.detail_number);
detail_remark = findViewById(R.id.detail_remark);
detail_sex = findViewById(R.id.detail_sex);
}
public void details_delete(View view) {
String id = getIntent().getStringExtra("id");
mySQLiteOpenHelper.dl_delete(id);
ToastUtil.toastShort(DetailsActivity.this,"联系人删除成功!");
Intent intent = new Intent(DetailsActivity.this,UserAbActivity.class);
startActivity(intent);
}
public void details_update(View view) {
String id = getIntent().getStringExtra("id");
Intent intent = new Intent(DetailsActivity.this,UpdateActivity.class);
intent.putExtra("id",id);
startActivity(intent);
}
}
package com.example.addressbook;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import com.example.addressbook.Util.ToastUtil;
public class AddActivity extends AppCompatActivity {
private EditText add_name,add_number,add_remark;
private RadioButton rb_man,rb_woman;
private MySQLiteOpenHelper mySQLiteOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
init();
}
private void init(){
add_name=findViewById(R.id.add_name);
add_number=findViewById(R.id.add_number);
add_remark=findViewById(R.id.add_remark);
rb_man=findViewById(R.id.rb_man);
rb_woman=findViewById(R.id.rb_woman);
}
public void im_add(View view) {
Intent intent = getIntent();
String id = intent.getStringExtra("id");
String name=add_name.getText().toString().trim();
String number=add_number.getText().toString().trim();
String remark=add_remark.getText().toString().trim();
if(name.isEmpty()){
ToastUtil.toastShort(AddActivity.this,"请输入姓名!");
}else if(number.isEmpty()){
ToastUtil.toastShort(AddActivity.this,"请输入手机号!");
}else if(remark.isEmpty()){
ToastUtil.toastShort(AddActivity.this,"请输入备注!");
}
String sex="女";
if(rb_man.isChecked()){
sex="男";
}
mySQLiteOpenHelper.add_data(name,number,sex,remark);
ToastUtil.toastShort(AddActivity.this,"添加成功");
}
}
package com.example.addressbook;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import com.example.addressbook.Util.ToastUtil;
import com.example.addressbook.bean.People;
public class UpdateActivity extends AppCompatActivity {
private EditText up_name,up_number,up_remark;
private RadioButton rb_man,rb_woman;
private MySQLiteOpenHelper mySQLiteOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_updateactivity);
mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
init();
Intent intent = getIntent();
String id = intent.getStringExtra("id");
People people=mySQLiteOpenHelper.getOnePeople(id);
up_name.setText(people.getName());
up_number.setText(people.getPhoneNumber());
if(people.getSex().equals("男")){
rb_man.setChecked(true);
}else{
rb_woman.setChecked(true);
}
up_remark.setText(people.getRemark());
}
private void init(){
up_name=findViewById(R.id.up_name);
up_number=findViewById(R.id.up_number);
up_remark=findViewById(R.id.up_remark);
rb_man=findViewById(R.id.rb_man);
rb_woman=findViewById(R.id.rb_woman);
}
public void im_update(View view) {
Intent intent = getIntent();
String id = intent.getStringExtra("id");
String name=up_name.getText().toString().trim();
String number=up_number.getText().toString().trim();
String sex="女";
if(rb_man.isChecked()){
sex="男";
}
String remark=up_remark.getText().toString().trim();
if(name.isEmpty()){
ToastUtil.toastShort(UpdateActivity.this,"请输入姓名!");
}else if(number.isEmpty()){
ToastUtil.toastShort(UpdateActivity.this,"请输入手机号!");
}else if(remark.isEmpty()){
ToastUtil.toastShort(UpdateActivity.this,"请输入备注!");
}
Intent intent1 = new Intent(UpdateActivity.this,UserAbActivity.class);
startActivity(intent1);
mySQLiteOpenHelper.updateData(name,number,sex,remark,id);
ToastUtil.toastShort(UpdateActivity.this,"更改成功");
// People people = new People();
// people.setName(name);
// people.setPhoneNumber(number);
// people.setRemark(remark);
// people.setSex(sex);
//
// long rowId = mySQLiteOpenHelper.updateData(people);
// if(rowId>0){
// //工具类ToastUtil的使用
// Intent intent = new Intent(UpdateActivity.this,DetailsActivity.class);
// startActivity(intent);
// ToastUtil.toastShort(this,"更新成功!");
// }else{
// ToastUtil.toastShort(this,"没有数据被更新!");
// }
}
}
package com.example.addressbook;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.os.Bundle;
import android.widget.ListView;
import com.example.addressbook.adapter.PeopleAdapter;
import com.example.addressbook.adapter.UserAdapter;
import com.example.addressbook.bean.People;
import com.example.addressbook.bean.User;
import java.util.Comparator;
import java.util.List;
public class ManagerAbActivity extends AppCompatActivity {
private MySQLiteOpenHelper mySQLiteOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manager_ab);
mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
//加载列表
List result = mySQLiteOpenHelper.getAllUser();
ListView listView = findViewById(R.id.book);
if(result.size()==0){
listView.setAdapter(null);//无显示
}else{
UserAdapter userAdapter = new UserAdapter(ManagerAbActivity.this,result);
listView.setAdapter(userAdapter);
}
}
}
package com.example.addressbook;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.addressbook.Util.ToastUtil;
import com.example.addressbook.bean.People;
import com.example.addressbook.bean.User;
public class ManagerImActivity extends AppCompatActivity {
private EditText user_name;
private EditText user_password;
private MySQLiteOpenHelper mySQLiteOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manager_im);
mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
init();
//信息同步
Intent intent = getIntent();
String id = intent.getStringExtra("id");
User user=mySQLiteOpenHelper.getOneUser(id);
user_name.setText(user.getName());
user_password.setText(user.getPassword());
}
private void init(){
user_name = findViewById(R.id.user_name);
user_password = findViewById(R.id.user_passowrd);
}
public void user_reset(View view) {
Intent intent = getIntent();
String id = intent.getStringExtra("id");
String name=user_name.getText().toString().trim();
String password=user_password.getText().toString().trim();
if(name.isEmpty()){
ToastUtil.toastShort(ManagerImActivity.this,"请输入姓名!");
}else if(password.isEmpty()){
ToastUtil.toastShort(ManagerImActivity.this,"请输入密码!");
}
Intent intent1 = new Intent(ManagerImActivity.this,ManagerAbActivity.class);
startActivity(intent1);
mySQLiteOpenHelper.updateUser(name,password,id);
ToastUtil.toastShort(ManagerImActivity.this,"更改成功");
}
public void user_delete(View view) {
String id = getIntent().getStringExtra("id");
mySQLiteOpenHelper.user_delete(id);
ToastUtil.toastShort(ManagerImActivity.this,"联系人删除成功!");
Intent intent = new Intent(ManagerImActivity.this,ManagerAbActivity.class);
startActivity(intent);
}
}