NotePad
基于原有的代码进行功能扩展
原有项目代码: notepad-master
本项目在原有项目的基础上增加了时间戳显示和笔记查询两个基础功能,以及美化UI和更改记事本的背景两个扩展功能
项目结构和功能
-
主要的类:
NotesList类 应用程序的入口,笔记本的首页面会显示笔记的列表
NoteEditor类 编辑笔记内容的Activity
TitleEditor类 编辑笔记标题的Activity
NotePadProvider类 这是笔记本应用的ContentProvider
NoteColor类 用来选择颜色
NoteSearch类 用于实现笔记查询
MyCursorAdapter类 继承SimpleCursorAdapter -
主要的布局文件:
note_editor.xml 笔记主页面布局
noteslist_item.xml 笔记主页面每个列表项布局
title_editor.xml 修改笔记主题布局
note_search.xml 笔记内容查询布局
note_color.xml 对选择颜色界面进行布局 -
主要的菜单文件:
editor_options_menu.xml 编辑笔记内容的菜单布局
list_context_menu.xml 笔记内容编辑上下文菜单布局
list_options_menu.xml 笔记主页面可选菜单布局
1.NotesList中显示条目增加时间戳显示
1.1.第一步:
修改NotesList.java
中PROJECTION的内容,添加modif字段,使其在后面的搜索中才能从SQLite中读取修改时间的字段。
private static final String[] PROJECTION = new String[] {
NotePad.Notes._ID, // 0
NotePad.Notes.COLUMN_NAME_TITLE, // 1
//Extended:display time, color
NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE, // 2
NotePad.Notes.COLUMN_NAME_BACK_COLOR
};
1.2.第二步:
修改适配器内容,增加dataColumns中装配到ListView的内容,因此要同时增加一个文本框来存放时间。
final String[] dataColumns = {
NotePad.Notes.COLUMN_NAME_TITLE , NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE} ;
int[] viewIDs = {
android.R.id.text1, R.id.text2};
1.3.第三步:
修改layout文件夹中noteslist_item.xml
的内容,增加一个textview组件,因为有两个组件,所以要相应的为他们添加一个布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:paddingBottom="3dip">
<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:singleLine="true"
/>
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:singleLine="true"
/>
</LinearLayout>
1.4.第四步:
修改NoteEditor.java
中updateNote方法中的时间类型。
private final void updateNote(String text, String title) {
// Sets up a map to contain values to be updated in the provider.
ContentValues values = new ContentValues();
Long now = Long.valueOf(System.currentTimeMillis());
SimpleDateFormat sf = new SimpleDateFormat("yy/MM/dd HH:mm");
Date d = new Date(now);
String format = sf.format(d);
values.put(NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE, format);
1.5.功能展示:
2.添加笔记查询功能(根据标题查询)
2.1.第一步:
搜索组件在主页面的菜单选项中,在list_options_menu.xml
布局文件中添加搜索功能。
<item
android:id="@+id/menu_search"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/menu_search"
android:showAsAction="always" />
2.2.第二步:
新建一个查找笔记内容的布局文件note_search.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">
<SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false"
/>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout