Android Studio完成记事本案例
时间: 2023-11-21 22:56:17 浏览: 205
android实现的记事本
为了完成记事本案例,我们需要进行以下步骤:
1.创建一个实体类Note,用于存放每个记录的内容和时间属性。
```java
public class Note {
private String content;
private String time;
public Note() {}
public Note(String content, String time) {
this.content = content;
this.time = time;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
```
2.创建一个用于显示记录列表的布局文件note_item.xml。
```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/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
```
3.创建一个用于添加新记录的布局文件activity_add_note.xml。
```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="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入记录内容" />
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加" />
</LinearLayout>
```
4.创建一个用于显示记录列表的Activity类MainActivity。
```java
public class MainActivity extends AppCompatActivity {
private List<Note> noteList = new ArrayList<>();
private RecyclerView recyclerView;
private NoteAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new NoteAdapter(noteList);
recyclerView.setAdapter(adapter);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, AddNoteActivity.class);
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
String content = data.getStringExtra("content");
String time = data.getStringExtra("time");
Note note = new Note(content, time);
noteList.add(note);
adapter.notifyItemInserted(noteList.size() - 1);
}
}
private class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.ViewHolder> {
private List<Note> noteList;
private NoteAdapter(List<Note> noteList) {
this.noteList = noteList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_item, parent, false);
final ViewHolder holder = new ViewHolder(view);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = holder.getAdapterPosition();
Note note = noteList.get(position);
Toast.makeText(MainActivity.this, note.getContent(), Toast.LENGTH_SHORT).show();
}
});
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Note note = noteList.get(position);
holder.tvContent.setText(note.getContent());
holder.tvTime.setText(note.getTime());
}
@Override
public int getItemCount() {
return noteList.size();
}
private class ViewHolder extends RecyclerView.ViewHolder {
private TextView tvContent;
private TextView tvTime;
private ViewHolder(@NonNull View itemView) {
super(itemView);
tvContent = itemView.findViewById(R.id.tv_content);
tvTime = itemView.findViewById(R.id.tv_time);
}
}
}
}
```
5.创建一个用于添加新记录的Activity类AddNoteActivity。
```java
public class AddNoteActivity extends AppCompatActivity {
private EditText etContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_note);
etContent = findViewById(R.id.et_content);
Button btnAdd = findViewById(R.id.btn_add);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String content = etContent.getText().toString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
String time = sdf.format(new Date());
Intent intent = new Intent();
intent.putExtra("content", content);
intent.putExtra("time", time);
setResult(RESULT_OK, intent);
finish();
}
});
}
}
```
以上就是Android Studio完成记事本案例的步骤。如果您需要更详细的代码实现,请参考相关的Android开发教程。
阅读全文