Android studio做一个个人笔记小程序详细代码
时间: 2023-06-14 20:08:20 浏览: 149
安装 | Android studio 3.5.2安装教程
5星 · 资源好评率100%
以下是一个简单的Android Studio个人笔记小程序的代码实现,代码注释已经做了详细说明,希望对你有所帮助:
1. MainActivity.java
```
package com.example.notes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private Button addNoteButton;
private ArrayList<Note> notesList; // 用于存储笔记数据的列表
private NotesAdapter notesAdapter; // 用于将数据绑定到recyclerview上的适配器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
addNoteButton = findViewById(R.id.addNoteButton);
// 设置recyclerview的布局管理器
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// 初始化笔记列表
notesList = new ArrayList<>();
// 创建适配器并将其绑定到recyclerview上
notesAdapter = new NotesAdapter(notesList);
recyclerView.setAdapter(notesAdapter);
// 监听添加笔记按钮的点击事件
addNoteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AddNoteActivity.class);
startActivityForResult(intent, 1);
}
});
}
// 处理添加笔记界面返回的数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
String title = data.getStringExtra("title");
String content = data.getStringExtra("content");
// 将新笔记添加到列表中
notesList.add(new Note(title, content));
notesAdapter.notifyDataSetChanged(); // 刷新recyclerview
}
}
}
```
2. Note.java
```
package com.example.notes;
public class Note {
private String title;
private String content;
public Note(String title, String content) {
this.title = title;
this.content = content;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
```
3. NotesAdapter.java
```
package com.example.notes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class NotesAdapter extends RecyclerView.Adapter<NotesViewHolder> {
private ArrayList<Note> notesList;
public NotesAdapter(ArrayList<Note> notesList) {
this.notesList = notesList;
}
@NonNull
@Override
public NotesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_item, parent, false);
return new NotesViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull NotesViewHolder holder, int position) {
Note note = notesList.get(position);
holder.bind(note);
}
@Override
public int getItemCount() {
return notesList.size();
}
}
```
4. NotesViewHolder.java
```
package com.example.notes;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class NotesViewHolder extends RecyclerView.ViewHolder {
private TextView titleTextView;
private TextView contentTextView;
public NotesViewHolder(@NonNull View itemView) {
super(itemView);
titleTextView = itemView.findViewById(R.id.titleTextView);
contentTextView = itemView.findViewById(R.id.contentTextView);
}
public void bind(Note note) {
titleTextView.setText(note.getTitle());
contentTextView.setText(note.getContent());
}
}
```
5. AddNoteActivity.java
```
package com.example.notes;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AddNoteActivity extends AppCompatActivity {
private EditText titleEditText;
private EditText contentEditText;
private Button saveButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_note);
titleEditText = findViewById(R.id.titleEditText);
contentEditText = findViewById(R.id.contentEditText);
saveButton = findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("title", titleEditText.getText().toString());
intent.putExtra("content", contentEditText.getText().toString());
setResult(RESULT_OK, intent); // 设置结果码和返回数据
finish(); // 关闭当前activity
}
});
}
}
```
6. note_item.xml
```
<?xml version="1.0" encoding="utf-8"?>
<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/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/contentTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textSize="16sp" />
</LinearLayout>
```
7. activity_main.xml
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/addNoteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Note"
android:layout_margin="16dp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
```
8. activity_add_note.xml
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".AddNoteActivity">
<EditText
android:id="@+id/titleEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title"
android:textSize="20sp"
android:textStyle="bold"
android:padding="16dp" />
<EditText
android:id="@+id/contentEditText"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="Content"
android:textSize="16sp"
android:padding="16dp" />
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:layout_gravity="end"
android:layout_margin="16dp"/>
</LinearLayout>
```
以上就是一个简单的Android Studio个人笔记小程序的代码实现,希望对你有所帮助。
阅读全文