**1.**项目布局设计
页面1:学生信息添加页面
采用线性布局,页面中控件包含TextView、editView、Button等。
布局核心代码如下:
html
<?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"
android:background="@drawable/addbg"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="信息添加页面"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_gravity="center"
android:layout_margin="80dp"/>
<EditText
android:id="@+id/editText_onesno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="学号"
android:textSize="25sp"/>
<EditText
android:id="@+id/editText_onename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名"
android:textSize="25sp"/>
<EditText
android:id="@+id/editText_onesex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="性别"
android:textSize="25sp"/>
<EditText
android:id="@+id/editText_onepro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="专业班级"
android:textSize="25sp"/>
<EditText
android:id="@+id/editText_onedep"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="所属系部"
android:textSize="25sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_oneadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"
android:textSize="25sp"
android:layout_weight="1"/>
<Button
android:id="@+id/button_oneclear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清除"
android:textSize="25sp"
android:layout_weight="1"/>
</LinearLayout>
<Button
android:id="@+id/button_onenext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一页"
android:textSize="25sp"
android:layout_gravity="right"
android:layout_marginTop="30dp"/>
</LinearLayout>
页面2:学生信息查询页面
采用线性布局,页面中控件包含TextView、editView、Button等。
布局核心代码如下:
html
<?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"
android:background="@drawable/querybg"
tools:context=".SecondActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="信息查询页面"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_gravity="center"
android:layout_margin="80dp"/>
<EditText
android:id="@+id/editText_twosno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入要查询的学号"
android:textSize="25sp"/>
<Button
android:id="@+id/button_twoquery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询"
android:textSize="25sp"/>
<TextView
android:id="@+id/textView_tworesult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示查询结果"
android:textSize="25sp" />
<Button
android:id="@+id/button_twonext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一页"
android:textSize="25sp"
android:layout_gravity="right"
android:layout_marginTop="30dp"/>
</LinearLayout>
页面3:学生信息修改页面
采用线性布局,页面中控件包含TextView、editView、Button等。
布局核心代码如下:
html
<?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"
android:background="@drawable/modifybg"
tools:context=".thirdActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="信息修改页面"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_gravity="center"
android:layout_margin="80dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="30dp">
<EditText
android:id="@+id/editText_threeinputsno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入要查询的学号"
android:textSize="25sp"/>
<Button
android:id="@+id/button_threequery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询"
android:textSize="25sp"/>
</LinearLayout>
<EditText
android:id="@+id/editText_threesno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="学号"
android:textSize="25sp"/>
<EditText
android:id="@+id/editText_threename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名"
android:textSize="25sp"/>
<EditText
android:id="@+id/editText_threedep"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="所属系部"
android:textSize="25sp"/>
<Button
android:id="@+id/button_threemodify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"
android:textSize="25sp"
android:layout_gravity="right"
android:layout_marginTop="30dp"/>
<Button
android:id="@+id/button_threenext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一页"
android:textSize="25sp"
android:layout_gravity="right"/>
</LinearLayout>
页面4:学生信息删除页面
采用线性布局,页面中控件包含TextView、editView、Button等。
布局核心代码如下:
html
<?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"
android:background="@drawable/deletebg"
tools:context=".fourActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="信息删除页面"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_gravity="center"
android:layout_margin="80dp"/>
<EditText
android:id="@+id/editText_foursno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入要删除的学号"
android:textSize="25sp"/>
<Button
android:id="@+id/button_fourdelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
android:textSize="25sp"
android:layout_gravity="right"/>
</LinearLayout>
2 . 系统后台实现代码
页面1:信息添加功能核心代码如下:
java
package com.example.word;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText ed_onesno,ed_onename,ed_onesex,ed_onepro,ed_onedep;
private Button btn_oneadd,btn_onenext,btn_oneclear;
private MyDBOpenHelper myDBOpenHelper;//数据库帮助类对象
private SQLiteDatabase db;//数据库对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化方法
initview();
//下一页
btn_onenext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
//清楚
btn_oneclear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ed_onesno.setText("");
ed_onename.setText("");
ed_onesex.setText("");
ed_onepro.setText("");
ed_onedep.setText("");
}
});
//添加
btn_oneadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ContentValues values=new ContentValues();
values.put("sno",ed_onesno.getText().toString());
values.put("name",ed_onename.getText().toString());
values.put("sex",ed_onesex.getText().toString());
values.put("professional",ed_onepro.getText().toString());
values.put("department",ed_onedep.getText().toString());
db.insert("stu_info",null,values);
Toast.makeText(MainActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
}
});
}
//初始化
private void initview() {
ed_onesno=findViewById(R.id.editText_onesno);
ed_onename=findViewById(R.id.editText_onename);
ed_onesex=findViewById(R.id.editText_onesex);
ed_onepro=findViewById(R.id.editText_onepro);
ed_onedep=findViewById(R.id.editText_onedep);
btn_oneadd=findViewById(R.id.button_oneadd);
btn_oneclear=findViewById(R.id.button_oneclear);
btn_onenext=findViewById(R.id.button_onenext);
myDBOpenHelper=new MyDBOpenHelper(MainActivity.this);
db=myDBOpenHelper.getWritableDatabase();//获取数据库的读写权限
}
}
页面2:信息查询功能核心代码如下:
java
package com.example.word;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SecondActivity extends AppCompatActivity {
private EditText ed_twosno;
private Button btn_twoquery,btn_twonext;
private TextView txtresult;
private MyDBOpenHelper myDBOpenHelper;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initview();
//查询
btn_twoquery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = db.rawQuery("select * from stu_info where sno=?",new String[]{ed_twosno.getText().toString()});
if (cursor.getCount() !=0){
Toast.makeText(SecondActivity.this, "查询成功!", Toast.LENGTH_SHORT).show();
while (cursor.moveToNext()){
@SuppressLint("Range")
String rsno = cursor.getString(cursor.getColumnIndex("sno"));
@SuppressLint("Range")
String rname = cursor.getString(cursor.getColumnIndex("name"));
@SuppressLint("Range")
String rsex = cursor.getString(cursor.getColumnIndex("sex"));
@SuppressLint("Range")
String rpro = cursor.getString(cursor.getColumnIndex("professional"));
@SuppressLint("Range")
String rdep = cursor.getString(cursor.getColumnIndex("department"));
txtresult.setText(rsno+"\n"+rname+"\n"+rsex+"\n"+rpro+"\n"+rdep);
}
}else {
Toast.makeText(SecondActivity.this, "没有查询到该学号对应的学生信息", Toast.LENGTH_SHORT).show();
txtresult.setText("");
}
}
});
//下一页
btn_twonext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(SecondActivity.this,thirdActivity.class);
startActivity(intent);
}
});
}
private void initview() {
ed_twosno=findViewById(R.id.editText_twosno);
btn_twonext=findViewById(R.id.button_twonext);
btn_twoquery=findViewById(R.id.button_twoquery);
txtresult=findViewById(R.id.textView_tworesult);
myDBOpenHelper = new MyDBOpenHelper(SecondActivity.this);
db = myDBOpenHelper.getWritableDatabase();
}
}
页面3:信息修改功能核心代码如下:
java
package com.example.word;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.ConditionVariable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class thirdActivity extends AppCompatActivity {
private EditText ed_threeinput,ed_threesno,ed_threename,ed_threedep;
private Button btn_threequery,btn_threemodify,btn_threenext;
private MyDBOpenHelper myDBOpenHelper;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
ed_threeinput=findViewById(R.id.editText_threeinputsno);
ed_threesno=findViewById(R.id.editText_threesno);
ed_threename=findViewById(R.id.editText_threename);
ed_threedep=findViewById(R.id.editText_threedep);
btn_threequery=findViewById(R.id.button_threequery);
btn_threemodify=findViewById(R.id.button_threemodify);
btn_threenext=findViewById(R.id.button_threenext);
myDBOpenHelper = new MyDBOpenHelper(thirdActivity.this);
db = myDBOpenHelper.getWritableDatabase();
btn_threequery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor cursor = db.rawQuery("select * from stu_info where sno=?",new String[]{ed_threeinput.getText().toString()});
if (cursor.getCount() !=0){
Toast.makeText(thirdActivity.this, "查询成功!", Toast.LENGTH_SHORT).show();
while (cursor.moveToNext()){
@SuppressLint("Range")
String rsno = cursor.getString(cursor.getColumnIndex("sno"));
@SuppressLint("Range")
String rname = cursor.getString(cursor.getColumnIndex("name"));
@SuppressLint("Range")
String rdep = cursor.getString(cursor.getColumnIndex("department"));
ed_threesno.setText(rsno);
ed_threename.setText(rname);
ed_threedep.setText(rdep);
}
}else {
Toast.makeText(thirdActivity.this, "没有查询到该学号对应的学生信息", Toast.LENGTH_SHORT).show();
ed_threesno.setText("");
ed_threename.setText("");
ed_threedep.setText("");
}
}
});
btn_threemodify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ContentValues values=new ContentValues();
values.put("name",ed_threename.getText().toString());
values.put("department",ed_threedep.getText().toString());
db.update("stu_info",values,"sno=?",new String[]{ed_threeinput.getText().toString()});
Toast.makeText(thirdActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
}
});
btn_threenext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(thirdActivity.this,fourActivity.class);
startActivity(intent);
}
});
}
}
页面4:信息删除功能核心代码如下:
java
package com.example.word;
import androidx.appcompat.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class fourActivity extends AppCompatActivity {
private EditText ed_foursno;
private Button btn_fourdel;
private MyDBOpenHelper myDBOpenHelper;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_four);
ed_foursno=findViewById(R.id.editText_foursno);
btn_fourdel=findViewById(R.id.button_fourdelete);
myDBOpenHelper = new MyDBOpenHelper(fourActivity.this);
db = myDBOpenHelper.getWritableDatabase();
btn_fourdel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
db.delete("stu_info","sno=?",new String[]{ed_foursno.getText().toString()});
Toast.makeText(fourActivity.this, "删除成功!", Toast.LENGTH_SHORT).show();
}
});
}
}