用Android studio写出简易课程表应用程序,用户可以添加、修改、删除课程,并且可以设置每个课程的具体时间及地点,方便学生掌握自己的日程安排的代码
时间: 2023-11-22 20:55:45 浏览: 93
很高兴为您提供帮助,以下是一个简单的课程表应用程序的示例代码,您可以在 Android Studio 中运行它。请注意,这只是一个示例代码,您可以根据自己的需求进行修改和扩展。
MainActivity.java
```
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private ArrayList<String> courses;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
courses = new ArrayList<>();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, courses);
listView.setAdapter(adapter);
// Add course button
findViewById(R.id.addButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AddCourseActivity.class);
startActivity(intent);
}
});
// Modify or delete course
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String course = courses.get(position);
Intent intent = new Intent(MainActivity.this, ModifyCourseActivity.class);
intent.putExtra("course", course);
intent.putExtra("position", position);
startActivity(intent);
}
});
}
@Override
protected void onResume() {
super.onResume();
courses.clear();
courses.addAll(CourseManager.getInstance().getCourses());
adapter.notifyDataSetChanged();
}
}
```
AddCourseActivity.java
```
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AddCourseActivity extends AppCompatActivity {
private EditText nameEditText, timeEditText, locationEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_course);
nameEditText = findViewById(R.id.nameEditText);
timeEditText = findViewById(R.id.timeEditText);
locationEditText = findViewById(R.id.locationEditText);
Button addButton = findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameEditText.getText().toString();
String time = timeEditText.getText().toString();
String location = locationEditText.getText().toString();
if (name.isEmpty() || time.isEmpty() || location.isEmpty()) {
Toast.makeText(AddCourseActivity.this, "Please fill all fields", Toast.LENGTH_SHORT).show();
return;
}
CourseManager.getInstance().addCourse(new Course(name, time, location));
finish();
}
});
}
}
```
ModifyCourseActivity.java
```
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ModifyCourseActivity extends AppCompatActivity {
private EditText nameEditText, timeEditText, locationEditText;
private int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_modify_course);
nameEditText = findViewById(R.id.nameEditText);
timeEditText = findViewById(R.id.timeEditText);
locationEditText = findViewById(R.id.locationEditText);
String course = getIntent().getStringExtra("course");
position = getIntent().getIntExtra("position", 0);
String[] parts = course.split("\n");
nameEditText.setText(parts[0]);
timeEditText.setText(parts[1]);
locationEditText.setText(parts[2]);
Button modifyButton = findViewById(R.id.modifyButton);
modifyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameEditText.getText().toString();
String time = timeEditText.getText().toString();
String location = locationEditText.getText().toString();
if (name.isEmpty() || time.isEmpty() || location.isEmpty()) {
Toast.makeText(ModifyCourseActivity.this, "Please fill all fields", Toast.LENGTH_SHORT).show();
return;
}
CourseManager.getInstance().modifyCourse(position, new Course(name, time, location));
finish();
}
});
Button deleteButton = findViewById(R.id.deleteButton);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CourseManager.getInstance().deleteCourse(position);
finish();
}
});
}
}
```
Course.java
```
public class Course {
private String name;
private String time;
private String location;
public Course(String name, String time, String location) {
this.name = name;
this.time = time;
this.location = location;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Override
public String toString() {
return name + "\n" + time + "\n" + location;
}
}
```
CourseManager.java
```
import java.util.ArrayList;
public class CourseManager {
private static CourseManager instance;
private ArrayList<Course> courses;
private CourseManager() {
courses = new ArrayList<>();
}
public static CourseManager getInstance() {
if (instance == null) {
instance = new CourseManager();
}
return instance;
}
public void addCourse(Course course) {
courses.add(course);
}
public void modifyCourse(int position, Course course) {
courses.set(position, course);
}
public void deleteCourse(int position) {
courses.remove(position);
}
public ArrayList<String> getCourses() {
ArrayList<String> courseStrings = new ArrayList<>();
for (Course course : courses) {
courseStrings.add(course.toString());
}
return courseStrings;
}
}
```
activity_main.xml
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/addButton" />
<Button
android:id="@+id/addButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Course"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
```
activity_add_course.xml
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Course Name" />
<EditText
android:id="@+id/timeEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Course Time" />
<EditText
android:id="@+id/locationEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Course Location" />
<Button
android:id="@+id/addButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add" />
</LinearLayout>
```
activity_modify_course.xml
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Course Name" />
<EditText
android:id="@+id/timeEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Course Time" />
<EditText
android:id="@+id/locationEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Course Location" />
<Button
android:id="@+id/modifyButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Modify" />
<Button
android:id="@+id/deleteButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete" />
</LinearLayout>
```
阅读全文