用androidstudio写一个记事本至少3个Acitvity以上;必须使用到sqlite数据库;
时间: 2024-05-01 22:23:01 浏览: 90
好的,以下是一个简单的记事本应用,包含三个Activity,其中使用了SQLite数据库存储数据:
1. MainActivity:显示所有已创建的笔记列表,可点击进入编辑笔记页面或新建笔记页面。
2. EditNoteActivity:编辑已有笔记或新建笔记页面,可输入笔记标题和内容,并保存至数据库。
3. ViewNoteActivity:查看已有笔记页面,显示笔记标题和内容。
首先,在Android Studio中创建一个新项目,并添加以下依赖项:
```groovy
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.room:room-runtime:2.3.0'
annotationProcessor 'androidx.room:room-compiler:2.3.0'
```
然后,在`AndroidManifest.xml`文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
接下来,创建一个SQLiteOpenHelper类和一个Note实体类,用于在数据库中保存笔记数据:
```java
public class NoteOpenHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "notes.db";
private static final int DATABASE_VERSION = 1;
public NoteOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE notes (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, content TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS notes");
onCreate(db);
}
}
@Entity(tableName = "notes")
public class Note {
@PrimaryKey(autoGenerate = true)
private int id;
private String title;
private String content;
public Note() {
}
public Note(int id, String title, String content) {
this.id = id;
this.title = title;
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
```
接下来,创建一个NoteDao接口和一个NoteDatabase类,用于访问数据库:
```java
@Dao
public interface NoteDao {
@Query("SELECT * FROM notes ORDER BY _id DESC")
LiveData<List<Note>> getAllNotes();
@Query("SELECT * FROM notes WHERE _id = :noteId")
LiveData<Note> getNoteById(int noteId);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(Note note);
@Update
void update(Note note);
@Delete
void delete(Note note);
}
@Database(entities = {Note.class}, version = 1)
public abstract class NoteDatabase extends RoomDatabase {
private static NoteDatabase instance;
public abstract NoteDao noteDao();
public static synchronized NoteDatabase getInstance(Context context) {
if (instance == null) {
instance = Room.databaseBuilder(context.getApplicationContext(),
NoteDatabase.class, "note_database")
.fallbackToDestructiveMigration()
.build();
}
return instance;
}
}
```
接下来,创建MainActivity类,用于显示笔记列表和跳转到其他页面:
```java
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private NoteAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recycler_view);
mAdapter = new NoteAdapter(this, new ArrayList<>());
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, EditNoteActivity.class);
startActivity(intent);
}
});
NoteDatabase.getInstance(this).noteDao().getAllNotes().observe(this, new Observer<List<Note>>() {
@Override
public void onChanged(List<Note> notes) {
mAdapter.setNotes(notes);
}
});
}
public void editNoteClicked(Note note) {
Intent intent = new Intent(MainActivity.this, EditNoteActivity.class);
intent.putExtra(EditNoteActivity.EXTRA_NOTE_ID, note.getId());
startActivity(intent);
}
public void viewNoteClicked(Note note) {
Intent intent = new Intent(MainActivity.this, ViewNoteActivity.class);
intent.putExtra(ViewNoteActivity.EXTRA_NOTE_ID, note.getId());
startActivity(intent);
}
}
```
接下来,创建EditNoteActivity类,用于编辑或新建笔记:
```java
public class EditNoteActivity extends AppCompatActivity {
public static final String EXTRA_NOTE_ID = "com.example.notepad.EXTRA_NOTE_ID";
private EditText mTitleEditText;
private EditText mContentEditText;
private int mNoteId = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_note);
mTitleEditText = findViewById(R.id.title_edit_text);
mContentEditText = findViewById(R.id.content_edit_text);
Intent intent = getIntent();
if (intent != null && intent.hasExtra(EXTRA_NOTE_ID)) {
mNoteId = intent.getIntExtra(EXTRA_NOTE_ID, -1);
NoteDatabase.getInstance(this).noteDao().getNoteById(mNoteId).observe(this, new Observer<Note>() {
@Override
public void onChanged(Note note) {
if (note != null) {
mTitleEditText.setText(note.getTitle());
mContentEditText.setText(note.getContent());
}
}
});
}
Button saveButton = findViewById(R.id.save_button);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String title = mTitleEditText.getText().toString().trim();
String content = mContentEditText.getText().toString().trim();
if (TextUtils.isEmpty(title) || TextUtils.isEmpty(content)) {
Toast.makeText(EditNoteActivity.this, R.string.error_empty_fields, Toast.LENGTH_SHORT).show();
} else {
if (mNoteId == -1) {
Note note = new Note();
note.setTitle(title);
note.setContent(content);
NoteDatabase.getInstance(EditNoteActivity.this).noteDao().insert(note);
} else {
NoteDatabase.getInstance(EditNoteActivity.this).noteDao().getNoteById(mNoteId).observe(EditNoteActivity.this, new Observer<Note>() {
@Override
public void onChanged(Note note) {
if (note != null) {
note.setTitle(title);
note.setContent(content);
NoteDatabase.getInstance(EditNoteActivity.this).noteDao().update(note);
}
}
});
}
finish();
}
}
});
}
}
```
最后,创建ViewNoteActivity类,用于查看已有笔记:
```java
public class ViewNoteActivity extends AppCompatActivity {
public static final String EXTRA_NOTE_ID = "com.example.notepad.EXTRA_NOTE_ID";
private TextView mTitleTextView;
private TextView mContentTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_note);
mTitleTextView = findViewById(R.id.title_text_view);
mContentTextView = findViewById(R.id.content_text_view);
Intent intent = getIntent();
if (intent != null && intent.hasExtra(EXTRA_NOTE_ID)) {
int noteId = intent.getIntExtra(EXTRA_NOTE_ID, -1);
NoteDatabase.getInstance(this).noteDao().getNoteById(noteId).observe(this, new Observer<Note>() {
@Override
public void onChanged(Note note) {
if (note != null) {
mTitleTextView.setText(note.getTitle());
mContentTextView.setText(note.getContent());
}
}
});
}
}
}
```
这样,一个简单的记事本应用就完成了。当用户新建或编辑笔记时,应用会将笔记数据保存至SQLite数据库中,并在主页面上显示所有已创建的笔记列表。用户可以点击任何一个笔记进入查看笔记页面。
阅读全文