Android studio生成一个任务管理
时间: 2024-03-02 21:24:46 浏览: 82
应用程序的步骤:
1. 打开Android Studio并创建一个新项目。
2. 选择“Empty Activity”作为项目模板。
3. 在“app”模块的“build.gradle”文件中添加以下依赖项:
```
dependencies {
implementation 'com.android.support:recyclerview-v7:28.0.0'
}
```
4. 在“app”模块的“res”文件夹中创建一个新的布局文件“activity_main.xml”。
5. 在“activity_main.xml”中添加一个RecyclerView元素:
```
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
```
6. 在“app”模块的Java文件夹中创建一个新的Java类“Task”:
```
public class Task {
private String name;
private String description;
public Task(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
}
```
7. 在“app”模块的Java文件夹中创建一个新的Java类“TaskAdapter”:
```
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> {
private List<Task> tasks;
public TaskAdapter(List<Task> tasks) {
this.tasks = tasks;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.task_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Task task = tasks.get(position);
holder.nameTextView.setText(task.getName());
holder.descriptionTextView.setText(task.getDescription());
}
@Override
public int getItemCount() {
return tasks.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView nameTextView;
private TextView descriptionTextView;
public ViewHolder(View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.nameTextView);
descriptionTextView = itemView.findViewById(R.id.descriptionTextView);
}
}
}
```
8. 在“app”模块的“res”文件夹中创建一个新的布局文件“task_item.xml”:
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/nameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/descriptionTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"/>
</LinearLayout>
```
9. 在“app”模块的Java文件夹中创建一个新的Java类“MainActivity”:
```
public class MainActivity extends AppCompatActivity {
private List<Task> tasks = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tasks.add(new Task("Task 1", "Description 1"));
tasks.add(new Task("Task 2", "Description 2"));
tasks.add(new Task("Task 3", "Description 3"));
tasks.add(new Task("Task 4", "Description 4"));
tasks.add(new Task("Task 5", "Description 5"));
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new TaskAdapter(tasks));
}
}
```
10. 运行应用程序并查看任务列表。
阅读全文