NotePad 记事本应⽤功能扩展(添加时间戳、查询、UI 美化、修改背景)NotePad
基于原有的代码进⾏功能扩展
原有项⽬代码:
本项⽬在原有项⽬的基础上增加了时间戳显⽰和笔记查询两个基础功能,以及美化UI和更改记事本的背景两个扩展功能项⽬结构和功能
主要的类:
NotesList类 应⽤程序的⼊⼝,笔记本的⾸页⾯会显⽰笔记的列表
NoteEditor类 编辑笔记内容的Activity
TitleEditor类 编辑笔记标题的Activity
NotePadProvider类 这是笔记本应⽤的ContentProvider
NoteColor类 ⽤来选择颜⾊
NoteSearch类 ⽤于实现笔记查询MyCursorAdapter类 继承SimpleCursorAdapter
主要的布局⽂件:
l 笔记主页⾯布局
l 笔记主页⾯每个列表项布局
l 修改笔记主题布局
l 笔记内容查询布局l 对选择颜⾊界⾯进⾏布局
主要的菜单⽂件:
editor_l 编辑笔记内容的菜单布局
list_l 笔记内容编辑上下⽂菜单布局
list_l 笔记主页⾯可选菜单布局
1.NotesList 中显⽰条⽬增加时间戳显⽰
1.1.第⼀步:修改NotesList.java 中PROJECTION的内容,添加modif字段,使其在后⾯的搜索中才能从SQLite中读取修改时间的字段。1.
2.第⼆步:
修改适配器内容,增加dataColumns中装配到ListView的内容,因此要同时增加⼀个⽂本框来存放时间。
1.3.第三步:
修改layout⽂件夹中l的内容,增加⼀个textview组件,因为有两个组件,所以要相应的为他们添加⼀个布局。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
3
4
5
6
notepad++7final String [] dataColumns = { NotePad .Notes .COLUMN_NAME_TITLE , NotePad .Notes .COLUMN_NAME_MODIFICATION_DATE } ;int [] viewIDs = { R .id .text1, R .id .text2};
1
2
1.4.第四步:
修改NoteEditor.java 中updateNote⽅法中的时间类型。
1.5.功能展⽰:
2.添加笔记查询功能(根据标题查询)
2.1.第⼀步:
搜索组件在主页⾯的菜单选项中,在list_l 布局⽂件中添加搜索功能。
2.2.第⼆步:
新建⼀个查笔记内容的布局⽂件l 。
<LinearLayout android ="schemas.android/apk/res/android " layout_width ="match_parent " layout_height ="match_parent " orientation ="vertical " paddingLeft ="6dip " paddingRight ="6dip " paddingBottom ="3dip "> <TextView id ="@android:id/text1" layout_width ="match_parent " layout_height ="?android:attr/listPreferredItemHeight " textAppearance ="?android:attr/textAppearanceLarge " gravity ="center_vertical " paddingLeft ="5dip " singleLine ="true " /> <TextView id ="@+id/text2" layout_width ="match_parent " layout_height ="wrap_content " textAppearance ="?android:attr/textAppearanceLarge " gravity ="center_vertical " singleLine ="true " /></LinearLayout >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24private 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
2
3
4
5
6
7
8
9 <item id ="@+id/menu_search " icon ="@android:drawable/ic_menu_search " title ="@string/menu_search " showAsAction ="always " />
1
2
3
4
5
2.3.第三步:
在NotesList.java 中的onOptionsItemSelected⽅法中添加search查询的处理。
2.4.第四步:
新建⼀个NoteSearch.java ⽤于search功能的功能实现。
<?xml version="1.0" encoding="utf-8"?><LinearLayout android ="schemas.android/apk/res/android " layout_width ="match_parent " layout_height ="match_parent " orientation ="vertical "> <SearchView id ="@+id/search_view " layout_width ="match_parent " layout_height ="wrap_content " iconifiedByDefault ="false " /> <ListView id ="@+id/list_view " layout_width ="match_parent " layout_height ="wrap_content " /></LinearLayout >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 case R .id .menu_search : //Find function //startActivity(new Intent(Intent.ACTION_SEARCH, getIntent().getData())); Intent intent = new Intent (this , NoteSearch .class ); this .startActivity (intent ); return true ;
1
2
3
4
5
6package ;import Activity ;import Intent ;import Cursor ;import SQLiteDatabase ;import Bundle ;import ListView ;import SearchView ;import SimpleCursorAdapter ;import Toast ;public class NoteSearch extends Activity implements SearchView .OnQueryTextListener { ListView listView ; SQLiteDatabase sqLiteDatabase ; /** * The columns needed by the cursor adapter */ private static final String [] PROJECTION = new String []{ NotePad .Notes ._ID , // 0 NotePad .Notes .COLUMN_NAME_TITLE , // 1 NotePad .Notes .COLUMN_NAME_MODIFICATION_DATE //时间 }; public boolean onQueryTextSubmit (String query ) { Toast .makeText (this , "you choose:"+query , Toast .LENGTH_SHORT ).show (); return false ; } @Override protected void onCreate (Bundle savedInstanceState ) { super .onCreate (savedInstanceState ); setContentView (R .layout .note_search );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2.5.第五步:在清单⽂件l ⾥⾯注册NoteSearch。
2.6.
功能展⽰: setContentView (R .layout .note_search ); SearchView searchView = findViewById (R .id .search_view ); Intent intent = getIntent (); if (intent .getData () == null ) { intent .setData (NotePad .Notes .CONTENT_URI ); } listView = findViewById (R .id .list_view ); sqLiteDatabase = new NotePadProvider .DatabaseHelper (this ).getReadableDatabase (); //Set the searchview to display the search button searchView .setSubmitButtonEnabled (true ); //Set the prompt text displayed by default in this searchview searchView .setQueryHint ("search"); searchView .setOnQueryTextListener (this ); } public boolean onQueryTextChange (String string ) { String selection1 = NotePad .Notes .COLUMN_NAME_TITLE +" like ? or "+NotePad .Notes .COLUMN_NAME_NOTE +" like ?"; String [] selection2 = {"%"+string +"%","%"+string +"%"}; Cursor cursor = sqLiteDatabase .query ( NotePad .Notes .TABLE_NAME , PROJECTION , // The columns to return from the query selection1, // The columns for the where clause selection2, // The values for the where clause null , // don't group the rows null , // don't filter by row groups NotePad .Notes .DEFAULT_SORT_ORDER // The sort order ); /
/ The names of the cursor columns to display in the view, initialized to the title column String [] dataColumns = { NotePad .Notes .COLUMN_NAME_TITLE , NotePad .Notes .COLUMN_NAME_MODIFICATION_DATE } ; // The view IDs that will display the cursor columns, initialized to the TextView in // l int [] viewIDs = { R .id .text1, R .id .text2
}; // Creates the backing adapter for the ListView. SimpleCursorAdapter adapter = new SimpleCursorAdapter ( this , // The Context for the ListView R .layout .noteslist_item , // Points to the XML for a list item cursor , // The cursor to get items from dataColumns , viewIDs ); // Sets the ListView's adapter to be the cursor adapter that was just created. listView .setAdapter (adapter ); return true ; }}33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 <activity name =".NoteSearch " label ="@string/search_note " />
1
3.UI 美化
3.1.第⼀步:
给NotesList换个主题,把⿊⾊换成⽩⾊,在l中NotesList的Activity中添加。3.2.第⼆步:在NotePad.java 中添加:
3.3.第三步:
创建数据库表地⽅添加颜⾊的字段。
3.4.第四步:在NotePad.java 中定义:
3.5.第五步:在NotePadProvider.java 中添加对其相应的处理,
static中:
insert中:
3.6.第六步:⾃定义⼀个MyCursorAdapter.java 继承SimpleCursorAdapter,将颜⾊填充到ListView。 <activity name ="NotesList " label ="@string/title_notes_list " theme ="@android:style/Theme.Holo.Light ">
1
2 public static final String COLUMN_NAME_BACK_COLOR = "color";
1 public void onCreate (SQLiteDatabase db ) { db .execSQL ("CREATE TABLE " + NotePad .Notes .TABLE_NAME + " (" + NotePad .Notes ._ID + " INTEGER PRIMARY KEY," + NotePad .Notes .COLUMN_NAME_TITLE + " TEXT," + NotePad .Notes .COLUMN_NAME_NOTE + " TEXT," + NotePad .Notes .COLUMN_NAME_CREATE_DATE + " INTEGER," + NotePad .Notes .COLUMN_NAME_MODIFICATION_DATE + " INTEGER," + NotePad .Notes .COLUMN_
NAME_BACK_COLOR + " INTEGER" //color + ");"); }
1
2
3
4
5
6
7
8
9
10 public static final int DEFAULT_COLOR = 0; //white public static final int YELLOW_
COLOR = 1; //yellow public static final int BLUE_COLOR = 2; //blue public static final int GREEN_COLOR = 3; //green public static final int RED_COLOR = 4; //red
1
2
3
4
5 sNotesProjectionMap .put ( NotePad .Notes .COLUMN_NAME_BACK_COLOR , NotePad .Notes .COLUMN_NAME_BACK_COLOR );
1
2
3 // Create a new notepad. The background is white by default if (values .containsKey (NotePad .Notes .COLUMN_NAME_BACK_COLOR ) == false ) { values .put (NotePad .Notes .COLUMN_NAME_BACK_COLOR , NotePad .Notes .DEFAULT_COLOR ); }
1
2
3
4
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论