效果介绍
打开记事本app,点击右上角菜单
点击New note,编写第一个记事本,内容为This is first note,点击右上角菜单,点击Revert changes清空,点击delete删除当前记事本,点击Save保存
分别新建了两个记事本,标题默认为记事本内容
点进一个记事本,点击右上角菜单
点击Editor title编辑标题
编辑记事本标题为1
点击右上角菜单的Search按钮进行记事本搜索
修改记事本背景颜色,点进一个记事本,点击右上角菜单的Color
选择一种颜色作为背景色
在noteslist_item.xml中添加显示时间戳的TextView
在NotePadProvider.java中添加创建时间、修改时间
在onCreate方法中添加创建时间、修改时间
在NotesList.java中添加修改时间
在dataColumns和viewIDs中添加修改时间
在NotePadProvider.java中设置时间戳显示格式为yyyy.MM.dd HH:mm:ss
在NoteEditor.java中也要设置时间戳
运行结果截图:
在list_options_menu.xml中加入搜索功能的item
在NotesList.java中的onOptionsItemSelected方法加入搜索功能的case语句
在layout里新建搜索功能的布局文件note_search_list.xml,代码如下:
创建类NoteSearch.java,继承ListView,实现SearchView.OnQueryTextListener接口,加入创建时间
为查询文本框注册监听器
onQueryTextChange方法使用适配器SimpleCursorAdapter
最后再AndroidManifest.ml中注册搜索功能的类NoteSearch
运行结果截图:
在values中新建color.xml,定义背景的七种颜色
在layout中新建note_color.xml,在线性布局中创建七个颜色按钮
新建NoteColor.java,代码如下:
在AndroidManifest.xml中注册背景颜色类NoteColor
在NotePad.java中加入背景颜色字段
在NotePadProvider.java中的insert方法添加背景颜色的代码,默认颜色为白色
新建一个MyCursorAdapter.java,继承SimpleCursorAdapter,用cursor读取数据库的内容,并填充颜色
在editor_options_menu.xml中添加修改背景颜色
在NoteEditor.java的onOptionsItemSelected方法中添加背景颜色的case
运行结果截图:
在color.xml中定义两组颜色,分别表示日间和夜间的主题色
在styles.xml中定义两种主题,日间/夜间主题
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColor">@android:color/black</item>
<item name="mainBackground">@android:color/white</item>
</style>
<style name="NightAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/nightColorPrimary</item>
<item name="colorPrimaryDark">@color/nightColorPrimaryDark</item>
<item name="colorAccent">@color/nightColorAccent</item>
<item name="android:textColor">@android:color/white</item>
<item name="mainBackground">@color/nightColorPrimaryDark</item>
</style>
新建一个mainBackground.xml,用来表示背景色
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="mainBackground" format="color|reference"></attr>
</resources>
在布局中加入切换按钮
<Button
android:id="@+id/btn_theme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="切换日/夜间" />
判断是否有主题存储
// 默认是日间模式
private int theme = R.style.AppTheme;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 判断是否有主题存储
if(savedInstanceState != null){
theme = savedInstanceState.getInt("theme");
setTheme(theme);
}
Button btn_theme = (Button) findViewById(R.id.btn_theme);
btn_theme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
theme = (theme == R.style.AppTheme) ? R.style.NightAppTheme : R.style.AppTheme;
MainActivity.this.recreate();
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("theme", theme);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
theme = savedInstanceState.getInt("theme");
}
运行结果截图: