Skip to content

increChong/NotePad-master

Repository files navigation

NotePad

主要功能

  • 添加时间
  • 搜索功能

附加功能

  • 更改背景颜色

添加时间

首先,由于看不惯黑色背景,于是将NoteList的颜色修改与NoteEditor一致: 在AndroidManifest.xml中添加

android:theme="@android:style/Theme.Holo.Light"
  • 先在noteslist_item中添加能够显示时间的TextView:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content" > <TextViewxmlns:android="http://schemas.android.com/apk/res/android"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" /> <TextViewandroid:id="@+id/date"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceLarge"android:gravity="center_vertical"android:paddingLeft="5dip"android:textColor="#708090"android:textSize="20dp"android:singleLine="true"/> </LinearLayout> 
  • 修改notelist.java 的PROJECTION
privatestaticfinalString[] PROJECTION = newString[]{NotePad.Notes._ID, // 0NotePad.Notes.COLUMN_NAME_TITLE, // 1NotePad.Notes.COLUMN_NAME_CREATE_DATE }; 
  • 修改dataColumns和viewID相关的值
String[] dataColumns ={NotePad.Notes.COLUMN_NAME_TITLE ,NotePad.Notes.COLUMN_NAME_CREATE_DATE} ;
int[] viewIDs ={android.R.id.text1 ,R.id.date};
  • 向NotePadProvider数据存储中添加时间值
SimpleDateFormatformatter = newSimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); DatecurDate = newDate(System.currentTimeMillis()); Stringdate = formatter.format(curDate); values.put(NotePad.Notes.COLUMN_NAME_CREATE_DATE,date); 

功能实现后如下图所示:

搜索功能

  • 首先在list_options_menu.xml中添加一个搜索的选项:
<menuxmlns:android="http://schemas.android.com/apk/res/android"> <itemandroid:id="@+id/menu_search"android:icon="@drawable/search1"android:title="@string/menu_add"android:alphabeticShortcut="s"android:showAsAction="always" /> <!-- Thisisouronestandardapplicationaction (creatinganewnote). --> <itemandroid:id="@+id/menu_add"android:icon="@drawable/ic_menu_compose"android:title="@string/menu_add"android:alphabeticShortcut='a'android:showAsAction="always" /> <!-- Ifthereiscurrentlydataintheclipboard, thisaddsaPASTEmenuitemtothemenusothattheusercanpasteinthedata.. --> <itemandroid:id="@+id/menu_paste"android:icon="@drawable/ic_menu_compose"android:title="@string/menu_paste"android:alphabeticShortcut='p' /> </menu>
  • 创建一个SearchView成员变量
privateSearchViewsearchView;
  • 写一个设置SearchView的方法
privatevoidsetSearchView(Menumenu){//1.找到menuItem并动态设置SearchViewMenuItemitem = menu.getItem(0); searchView = newSearchView(this); item.setActionView(searchView); //2.设置搜索的背景为白色if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){item.collapseActionView()} searchView.setQuery("", false); //searchView.setBackgroundResource(R.drawable.ic_menu_edit);//3.设置为默认展开状态,图标在外面searchView.setIconifiedByDefault(true); searchView.setQueryHint("Search"); searchView.setOnQueryTextListener(newSearchView.OnQueryTextListener(){@OverridepublicbooleanonQueryTextSubmit(Stringquery){if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){searchView.onActionViewCollapsed()} returnfalse} @OverridepublicbooleanonQueryTextChange(StringnewText){if (!TextUtils.isEmpty(newText)){Cursorcursor = managedQuery( getIntent().getData(), // Use the default content URI for the provider.PROJECTION, // Return the note ID and title for each note.NotePad.Notes.COLUMN_NAME_TITLE + " LIKE '%" + newText + "%' ", // 相当于where语句null, // No where clause, therefore no where column values.NotePad.Notes.DEFAULT_SORT_ORDER// Use the default sort order. ); finalString[] dataColumn ={NotePad.Notes.COLUMN_NAME_CREATE_DATE, NotePad.Notes.COLUMN_NAME_TITLE}; // The view IDs that will display the cursor columns, initialized to the TextView in// noteslist_item.xmlint[] viewID ={R.id.date,android.R.id.text1}; SimpleCursorAdapteradapter1 = newSimpleCursorAdapter( NotesList.this, // The Context for the ListViewR.layout.noteslist_item, // Points to the XML for a list itemcursor, // The cursor to get items fromdataColumn, viewID ); //重新setListAdaptersetListAdapter(adapter1)} else{//恢复默认setListAdaptersetListAdapter(adapter)} returnfalse} })}

onQueryTextChange是监听搜索框的方法,根据输入文字改变SimpleCursorAdapter界面

  • 在onCreateOptionsMenu中添加setSearchView方法
publicbooleanonCreateOptionsMenu(Menumenu){// Inflate menu from XML resourceMenuInflaterinflater = getMenuInflater(); inflater.inflate(R.menu.list_options_menu, menu); setSearchView(menu); // Generate any additional actions that can be performed on the// overall list. In a normal install, there are no additional// actions found here, but this allows other applications to extend// our menu with their own actions.Intentintent = newIntent(null, getIntent().getData()); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, newComponentName(this, NotesList.class), null, intent, 0, null); returnsuper.onCreateOptionsMenu(menu)}

实现后界面如下:

更改背景颜色

  • 先在NoteList的菜单栏里添加一个改变背景颜色的选项

在list_option_menu中添加:

<itemandroid:id="@+id/bg"android:title="Background"android:alphabeticShortcut="b" />

效果如图:

  • 创建一个用来显示颜色选择的布局color.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="100dp"> <TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="#E1FFFF"android:id="@+id/LightCyan"android:clickable="true"android:layout_weight="1"/> <TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="#FFC0CB"android:id="@+id/Pink"android:layout_weight="1"/> <TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="#87CEFA"android:id="@+id/LightSkyBlue"android:layout_weight="1"/> <TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="#F8F8FF"android:id="@+id/GhostWhite"android:layout_weight="1"/> </LinearLayout>
  • 使用SharedPreferences来保存之前设置的颜色,在下次启动应用时设置

创建一个SharedPreferences变量和存储颜色的变量:

privateSharedPreferencesbg; privateStringpreferencescolor;

使用SharedPreferences保存颜色的方法:

privatevoidputColor(Stringcolor){SharedPreferencespreferences = getSharedPreferences("preferences", Context.MODE_PRIVATE); SharedPreferences.Editoreditor = preferences.edit(); editor.putString("color", color); editor.commit()}

在onCreate方法中设置,如果之前有设置过颜色,启动时应用,没有则使用默认颜色:

bg = getSharedPreferences("preferences", Context.MODE_PRIVATE); preferencescolor=bg.getString("color", ""); //设置Listview的背景色if(preferencescolor.equals("")){getListView().setBackgroundColor(Color.parseColor("#FFFFFF"))} else{getListView().setBackgroundColor(Color.parseColor(preferencescolor))}
  • 在onOptionItemSelected方法中设置Background的响应事件
publicbooleanonOptionsItemSelected(MenuItemitem){switch (item.getItemId()){caseR.id.menu_add: /* * Launches a new Activity using an Intent. The intent filter for the Activity * has to have action ACTION_INSERT. No category is set, so DEFAULT is assumed. * In effect, this starts the NoteEditor Activity in NotePad. */startActivity(newIntent(Intent.ACTION_INSERT, getIntent().getData())); returntrue; caseR.id.menu_paste: /* * Launches a new Activity using an Intent. The intent filter for the Activity * has to have action ACTION_PASTE. No category is set, so DEFAULT is assumed. * In effect, this starts the NoteEditor Activity in NotePad. */startActivity(newIntent(Intent.ACTION_PASTE, getIntent().getData())); returntrue; //Background的点击事件caseR.id.bg: finalAlertDialogalertDialog = newAlertDialog.Builder(NotesList.this).create(); alertDialog.show(); Windowwindow = alertDialog.getWindow(); window.setContentView(R.layout.color); TextViewcolor_LightCyan = (TextView)alertDialog.getWindow().findViewById(R.id.LightCyan); TextViewcolor_Pink = (TextView)alertDialog.getWindow().findViewById(R.id.Pink); TextViewcolor_LightSkyBlue = (TextView)alertDialog.getWindow().findViewById(R.id.LightSkyBlue); TextViewcolor_GhostWhite = (TextView)alertDialog.getWindow().findViewById(R.id.GhostWhite); color_LightCyan.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){preferencescolor="#E1FFFF"; getListView().setBackgroundColor(Color.parseColor(preferencescolor)); putColor(preferencescolor); alertDialog.dismiss()} }); color_Pink.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){preferencescolor="#FFC0CB"; getListView().setBackgroundColor(Color.parseColor(preferencescolor)); putColor(preferencescolor); alertDialog.dismiss()} }); color_LightSkyBlue.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){preferencescolor="#87CEFA"; getListView().setBackgroundColor(Color.parseColor(preferencescolor)); putColor(preferencescolor); alertDialog.dismiss()} }); color_GhostWhite.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){preferencescolor="#F8F8FF"; getListView().setBackgroundColor(Color.parseColor(preferencescolor)); putColor(preferencescolor); alertDialog.dismiss()} }); returntrue; default: returnsuper.onOptionsItemSelected(item)} }

alertDialog是用来显示刚才创建color.xml;然后给每个颜色设置点击事件,将色值传给preferencescolor并设置为背景

实现后如下图:

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages