android studio 备忘录日历完整代码
时间: 2024-12-27 18:25:02 浏览: 5
### Android Studio 中实现备忘录和日历功能的完整代码示例
#### 主要模块概述
为了帮助理解如何在Android Studio中构建具备日历查看、添加备忘录、闹钟提醒以及删除备忘录等功能的应用程序,下面提供了一个简化版的日历备忘录应用的核心组件说明及其部分代码片段[^2]。
#### 数据库操作 (SQLite)
##### 创建数据库辅助类 `DatabaseHelper.java`
```java
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "calendar.db";
private static final int DATABASE_VERSION = 1;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// 创建用于存储备忘录记录的数据表
String CREATE_TABLE_MEMOS = "CREATE TABLE memos (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"title TEXT NOT NULL," +
"description TEXT," +
"date TEXT NOT NULL);";
db.execSQL(CREATE_TABLE_MEMOS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 更新版本时的操作
db.execSQL("DROP TABLE IF EXISTS memos");
onCreate(db);
}
}
```
#### 添加新备忘录逻辑 (`AddMemoActivity.java`)
```java
// 导入必要的包...
import java.text.SimpleDateFormat;
import java.util.Date;
public class AddMemoActivity extends AppCompatActivity {
EditText etTitle, etDescription;
Button btnSave;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_memo);
etTitle = findViewById(R.id.et_title);
etDescription = findViewById(R.id.et_description);
btnSave = findViewById(R.id.btn_save);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveMemo();
}
});
}
private void saveMemo(){
String title = etTitle.getText().toString();
String description = etDescription.getText().toString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDateandTime = sdf.format(new Date());
ContentValues values = new ContentValues();
values.put("title", title);
values.put("description", description);
values.put("date", currentDateandTime);
SQLiteDatabase db = new DatabaseHelper(this).getWritableDatabase();
long newRowId = db.insert("memos", null, values);
Toast.makeText(AddMemoActivity.this,
"New Memo Added with ID:" + newRowId,
Toast.LENGTH_SHORT).show();
finish(); // 返回到主界面或其他指定位置
}
}
```
#### 显示所有备忘录列表 (`MainActivity.java`)
```java
// 导入必要包...
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{
ListView lvMemos;
SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvMemos = findViewById(R.id.lv_memos);
getSupportLoaderManager().initLoader(0,null,this);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, AddMemoActivity.class);
startActivity(intent);
}
});
registerForContextMenu(lvMemos);
}
@NonNull
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args){
return new CursorLoader(
this,
Uri.parse("content://com.example.calendarprovider/memos"),
null,
null,
null,
null
);
}
@Override
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor data){
adapter = new SimpleCursorAdapter(
this,
R.layout.list_item_memo,
data,
new String[]{"_id","title"},
new int[]{R.id.tv_id,R.id.tv_title},
0
);
lvMemos.setAdapter(adapter);
}
@Override
public void onLoaderReset(@NonNull Loader<Cursor> loader){
lvMemos.setAdapter(null);
}
...
}
```
以上展示了基于Android Studio平台下创建一个简单的日历备忘录应用程序所需的部分核心Java文件的内容。这些例子涵盖了从建立SQLite数据库连接至执行基本CRUD(Create, Read, Update, Delete)操作的过程,并且包含了用户交互的设计思路,比如通过浮动按钮触发新增加项的动作等[^3]。
阅读全文