用安卓studio实现考试管理系统,并给出源码
时间: 2024-03-05 15:49:31 浏览: 38
很抱歉,考试管理系统是一个较为复杂的应用,无法在此简单地给出完整的源代码。不过,我可以提供一些基本的实现思路和代码示例,供您参考:
1. 登录页面布局文件(activity_login.xml):
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<EditText
android:id="@+id/username_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"/>
<EditText
android:id="@+id/password_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"/>
<Button
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"/>
</LinearLayout>
```
2. 登录页面Java代码(LoginActivity.java):
```java
public class LoginActivity extends AppCompatActivity {
private EditText mUsernameEditText;
private EditText mPasswordEditText;
private Button mLoginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mUsernameEditText = findViewById(R.id.username_edittext);
mPasswordEditText = findViewById(R.id.password_edittext);
mLoginButton = findViewById(R.id.login_button);
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = mUsernameEditText.getText().toString();
String password = mPasswordEditText.getText().toString();
if (isValidUser(username, password)) {
Intent intent = new Intent(LoginActivity.this, MainMenuActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
});
}
private boolean isValidUser(String username, String password) {
// TODO: 根据数据库中的数据判断用户是否合法
return true;
}
}
```
3. SQLite数据库操作类(DBHelper.java):
```java
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "exam.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_STUDENT = "student";
private static final String TABLE_EXAM = "exam";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createStudentTableSql = "CREATE TABLE " + TABLE_STUDENT +
" (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"username TEXT NOT NULL, " +
"password TEXT NOT NULL)";
String createExamTableSql = "CREATE TABLE " + TABLE_EXAM +
" (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"student_id INTEGER NOT NULL, " +
"subject TEXT NOT NULL, " +
"score INTEGER NOT NULL)";
db.execSQL(createStudentTableSql);
db.execSQL(createExamTableSql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO: 数据库升级逻辑
}
public boolean insertStudent(String username, String password) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("username", username);
values.put("password", password);
long result = db.insert(TABLE_STUDENT, null, values);
db.close();
return result != -1;
}
public boolean updateStudent(String username, String password) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("password", password);
String selection = "username = ?";
String[] selectionArgs = { username };
int result = db.update(TABLE_STUDENT, values, selection, selectionArgs);
db.close();
return result > 0;
}
public boolean deleteStudent(String username) {
SQLiteDatabase db = getWritableDatabase();
String selection = "username = ?";
String[] selectionArgs = { username };
int result = db.delete(TABLE_STUDENT, selection, selectionArgs);
db.close();
return result > 0;
}
public boolean insertExam(int studentId, String subject, int score) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("student_id", studentId);
values.put("subject", subject);
values.put("score", score);
long result = db.insert(TABLE_EXAM, null, values);
db.close();
return result != -1;
}
public Cursor queryStudentByUsername(String username) {
SQLiteDatabase db = getReadableDatabase();
String[] columns = { "_id", "username", "password" };
String selection = "username = ?";
String[] selectionArgs = { username };
Cursor cursor = db.query(TABLE_STUDENT, columns, selection, selectionArgs, null, null, null);
return cursor;
}
public Cursor queryExamByStudentId(int studentId) {
SQLiteDatabase db = getReadableDatabase();
String[] columns = { "_id", "subject", "score" };
String selection = "student_id = ?";
String[] selectionArgs = { String.valueOf(studentId) };
Cursor cursor = db.query(TABLE_EXAM, columns, selection, selectionArgs, null, null, null);
return cursor;
}
}
```
4. 主菜单页面布局文件(activity_main_menu.xml):
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/exam_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="考试"/>
<Button
android:id="@+id/score_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="成绩"/>
<Button
android:id="@+id/logout_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注销"/>
</LinearLayout>
```
5. 主菜单页面Java代码(MainMenuActivity.java):
```java
public class MainMenuActivity extends AppCompatActivity {
private Button mExamButton;
private Button mScoreButton;
private Button mLogoutButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
mExamButton = findViewById(R.id.exam_button);
mScoreButton = findViewById(R.id.score_button);
mLogoutButton = findViewById(R.id.logout_button);
mExamButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainMenuActivity.this, ExamActivity.class);
startActivity(intent);
}
});
mScoreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainMenuActivity.this, ScoreActivity.class);
startActivity(intent);
}
});
mLogoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainMenuActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
});
}
}
```
6. 考试页面布局文件(activity_exam.xml):
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/subject_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="科目"/>
<EditText
android:id="@+id/score_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="分数"/>
<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"/>
</LinearLayout>
```
7. 考试页面Java代码(ExamActivity.java):
```java
public class ExamActivity extends AppCompatActivity {
private TextView mSubjectTextView;
private EditText mScoreEditText;
private Button mSubmitButton;
private DBHelper mDBHelper;
private int mStudentId;
private String mSubject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exam);
mSubjectTextView = findViewById(R.id.subject_textview);
mScoreEditText = findViewById(R.id.score_edittext);
mSubmitButton = findViewById(R.id.submit_button);
mDBHelper = new DBHelper(this);
mStudentId = 1; // TODO: 获取当前登录的学生ID
mSubject = "数学"; // TODO: 获取当前考试的科目
mSubjectTextView.setText(mSubject);
mSubmitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int score = Integer.parseInt(mScoreEditText.getText().toString());
mDBHelper.insertExam(mStudentId, mSubject, score);
Toast.makeText(ExamActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
finish();
}
});
}
}
```
8. 成绩页面布局文件(activity_score.xml):
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/subject_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="科目"/>
<TextView
android:id="@+id/score_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="分数"/>
</LinearLayout>
```
9. 成绩页面Java代码(ScoreActivity.java):
```java
public class ScoreActivity extends AppCompatActivity {
private TextView mSubjectTextView;
private TextView mScoreTextView;
private DBHelper mDBHelper;
private int mStudentId;
private String mSubject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score);
mSubjectTextView = findViewById(R.id.subject_textview);
mScoreTextView = findViewById(R.id.score_textview);
mDBHelper = new DBHelper(this);
mStudentId = 1; // TODO: 获取当前登录的学生ID
mSubject = "数学"; // TODO: 获取当前查询的科目
mSubjectTextView.setText(mSubject);
Cursor cursor = mDBHelper.queryExamByStudentId(mStudentId);
while (cursor.moveToNext()) {
String subject = cursor.getString(cursor.getColumnIndex("subject"));
int score = cursor.getInt(cursor.getColumnIndex("score"));
if (subject.equals(mSubject)) {
mScoreTextView.setText(String.valueOf(score));
break;
}
}
cursor.close();
}
}
```
需要注意的是,以上代码仅供参考,实际应用中还需要根据具体需求进行修改和调整。另外,数据库操作和数据安全等方面也需要格外注意。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)