好的,以下是在 Android Studio 中创建一个简单的课程表应用,实现添加课程信息的输入界面和课程表的显示界面的步骤。 创建一个新的 Android Studio 项目,选择 Empty Activity 模板。 在项目中创建两个新的 Activities,命名为 AddCourseActivity 和 CourseTableActivity。 在 AddCourseActivity 的布局文件中添加文本输入框和按钮,用于输入课程信息。这里以课程名称、授课教师、上课时间和上课地点为例:
时间: 2023-11-27 21:53:32 浏览: 107
Android课程设计简单实用课程表项目
好的,下面是在 Android Studio 中创建一个简单的课程表应用,实现添加课程信息的输入界面和课程表的显示界面的步骤:
1. 创建一个新的 Android Studio 项目,选择 Empty Activity 模板。
2. 在项目中创建两个新的 Activities,命名为 AddCourseActivity 和 CourseTableActivity。
3. 在 AddCourseActivity 的布局文件中添加文本输入框和按钮,用于输入课程信息。这里以课程名称、授课教师、上课时间和上课地点为例:
```
<EditText
android:id="@+id/edit_text_course_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="课程名称" />
<EditText
android:id="@+id/edit_text_teacher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="授课教师" />
<EditText
android:id="@+id/edit_text_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="上课时间" />
<EditText
android:id="@+id/edit_text_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="上课地点" />
<Button
android:id="@+id/button_add_course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加" />
```
4. 在 AddCourseActivity 的 Java 文件中,获取文本框中的输入信息,并将其保存到数据库中:
```
public class AddCourseActivity extends AppCompatActivity {
private EditText mEditTextCourseName;
private EditText mEditTextTeacher;
private EditText mEditTextTime;
private EditText mEditTextLocation;
private Button mButtonAddCourse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_course);
mEditTextCourseName = findViewById(R.id.edit_text_course_name);
mEditTextTeacher = findViewById(R.id.edit_text_teacher);
mEditTextTime = findViewById(R.id.edit_text_time);
mEditTextLocation = findViewById(R.id.edit_text_location);
mButtonAddCourse = findViewById(R.id.button_add_course);
mButtonAddCourse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String courseName = mEditTextCourseName.getText().toString();
String teacher = mEditTextTeacher.getText().toString();
String time = mEditTextTime.getText().toString();
String location = mEditTextLocation.getText().toString();
// 将课程信息保存到数据库中
}
});
}
}
```
5. 在 CourseTableActivity 的布局文件中添加一个 RecyclerView 控件,用于显示课程表信息:
```
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_course_table"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
6. 创建一个 Course 类,用于保存课程信息:
```
public class Course {
private String mCourseName;
private String mTeacher;
private String mTime;
private String mLocation;
public Course(String courseName, String teacher, String time, String location) {
mCourseName = courseName;
mTeacher = teacher;
mTime = time;
mLocation = location;
}
public String getCourseName() {
return mCourseName;
}
public void setCourseName(String courseName) {
mCourseName = courseName;
}
public String getTeacher() {
return mTeacher;
}
public void setTeacher(String teacher) {
mTeacher = teacher;
}
public String getTime() {
return mTime;
}
public void setTime(String time) {
mTime = time;
}
public String getLocation() {
return mLocation;
}
public void setLocation(String location) {
mLocation = location;
}
}
```
7. 创建一个 CourseAdapter 类,用于将课程信息绑定到 RecyclerView 中:
```
public class CourseAdapter extends RecyclerView.Adapter<CourseAdapter.CourseViewHolder> {
private List<Course> mCourseList;
public CourseAdapter(List<Course> courseList) {
mCourseList = courseList;
}
@NonNull
@Override
public CourseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_course, parent, false);
return new CourseViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull CourseViewHolder holder, int position) {
Course course = mCourseList.get(position);
holder.mTextViewCourseName.setText(course.getCourseName());
holder.mTextViewTeacher.setText(course.getTeacher());
holder.mTextViewTime.setText(course.getTime());
holder.mTextViewLocation.setText(course.getLocation());
}
@Override
public int getItemCount() {
return mCourseList.size();
}
static class CourseViewHolder extends RecyclerView.ViewHolder {
private TextView mTextViewCourseName;
private TextView mTextViewTeacher;
private TextView mTextViewTime;
private TextView mTextViewLocation;
public CourseViewHolder(@NonNull View itemView) {
super(itemView);
mTextViewCourseName = itemView.findViewById(R.id.text_view_course_name);
mTextViewTeacher = itemView.findViewById(R.id.text_view_teacher);
mTextViewTime = itemView.findViewById(R.id.text_view_time);
mTextViewLocation = itemView.findViewById(R.id.text_view_location);
}
}
}
```
8. 在 CourseTableActivity 的 Java 文件中,将 RecyclerView 和 CourseAdapter 绑定,并从数据库中获取课程信息:
```
public class CourseTableActivity extends AppCompatActivity {
private RecyclerView mRecyclerViewCourseTable;
private CourseAdapter mCourseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_table);
mRecyclerViewCourseTable = findViewById(R.id.recycler_view_course_table);
mRecyclerViewCourseTable.setLayoutManager(new LinearLayoutManager(this));
List<Course> courseList = new ArrayList<>();
// 从数据库中获取课程信息,添加到 courseList 中
mCourseAdapter = new CourseAdapter(courseList);
mRecyclerViewCourseTable.setAdapter(mCourseAdapter);
}
}
```
这样,一个简单的课程表应用就完成了。用户可以在 AddCourseActivity 中输入课程信息,保存到数据库中,然后在 CourseTableActivity 中显示出来。
阅读全文