简介
实现了NotePad的基础功能时间戳和搜索,扩展了UI美化。
NotePad源码下载
可能bug及解决方式
- 下载源码解压并在AndroidStudio中打开后,点击build可能会报错
此时,在build.gradle中添加 google();
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.2"
}
}
allprojects {
repositories {
google()
mavenCentral ()
maven {url 'https://dl.bintray.com/jetbrains/anko'}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
修改 gradle 文件夹下的 gradle-wrapper.properties 的 distributionUrl 属性为
https://services.gradle.org/distributions/gradle-7.0.2-bin.zip 然后检查SDK版本 修改app下build.gradle的SDK版本一致
功能代码详解
时间戳功能
- 在主页面的每个列表项中添加时间戳的位置,即在notelist_item.xml布局文件中添加一个
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="12dp"
android:gravity="center_vertical"
android:paddingLeft="10dip"
android:singleLine="true"
android:layout_weight="1"
android:layout_margin="0dp"
/>
- 需要修改这个方法中的时间戳格式
NotePadProvider中的insert方法:
Long now = Long.valueOf(System.currentTimeMillis());
Date date = new Date(now);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
String dateFormat = simpleDateFormat.format(date);
if(values.containsKey(NotePad.Notes.COLUMN_NAME_CREATE_DATE) == false) {
values.put(NotePad.Notes.COLUMN_NAME_CREATE_DATE, dateFormat);
}
if (values.containsKey(NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE) == false) {
values.put(NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE, dateFormat);
}
NoteEditor中的updateNote方法:
long now = System.currentTimeMillis();
Date date = new Date(now);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
String dateFormat = simpleDateFormat.format(date);
values.put(NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE, dateFormat);
- NoteList的修改
如果我们要加入时间戳,必须要将修改时间的列也投影出来. 所以我们将PROJECTION添加上修改时间:
private static final String[] PROJECTION = new String[] {
NotePad.Notes._ID,
NotePad.Notes.COLUMN_NAME_TITLE,
NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE,
};
PROJECTION只是定义了需要被取出来的数据列,而之后用Cursor进行数据库查询,再之后用Adapter进行装填。我们需要将显示列dataColumns和他们的viewIDs加入修改时间这一属性:
String[] dataColumns = { NotePad.Notes.COLUMN_NAME_TITLE, NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE }
int[] viewIDs = { android.R.id.text1, R.id.text2 }
- 实现结果如下:
搜索功能
- 搜索组件在主页面的菜单选项中,所以应在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" />
- 新建一个查找笔记内容的布局文件note_search.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<SearchView
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
- 在NoteList类中的onOptionsItemSelected方法中添加search查询的处理(跳转)
case R.id.menu_search:
Intent intent = new Intent(this, NoteSearch.class);
this.startActivity(intent);
return true;
- 新建一个NoteSearch类用于search功能的功能实现
package com.example.android.notepad;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class NoteSearch extends Activity implements SearchView.OnQueryTextListener
{
ListView listView;
SQLiteDatabase sqLiteDatabase;
private static final String[] PROJECTION = new String[]{
NotePad.Notes._ID,
NotePad.Notes.COLUMN_NAME_TITLE,
NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE
};
public boolean onQueryTextSubmit(String query) {
Toast.makeText(this, "您选择的是:"+query, Toast.LENGTH_SHORT).show();
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note_search);
SearchView searchView = (SearchView) findViewById(R.id.search);
Intent intent = getIntent();
if (intent.getData() == null) {
intent.setData(NotePad.Notes.CONTENT_URI);
}
listView = (ListView) findViewById(R.id.list);
sqLiteDatabase = new NotePadProvider.DatabaseHelper(this).getReadableDatabase();
searchView.setSubmitButtonEnabled(true);
searchView.setQueryHint("查找");
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,
selection1,
selection2,
null,
null,
NotePad.Notes.DEFAULT_SORT_ORDER
);
String[] dataColumns = {
NotePad.Notes.COLUMN_NAME_TITLE,
NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE
} ;
int[] viewIDs = {
android.R.id.text1,
android.R.id.text2
};
SimpleCursorAdapter adapter
= new SimpleCursorAdapter(
this,
R.layout.noteslist_item,
cursor,
dataColumns,
viewIDs
);
listView.setAdapter(adapter);
return true;
}
}
- 最后要在清单文件AndroidManifest.xml里面注册NoteSearch,否则无法实现界面的跳转
<activity android:name=".NoteSearch" android:label="@string/menu_search" />
- 实现结果如下:
UI美化
- 先给NoteList换个主题,把黑色换成白色,在AndroidManifest.xml中NotesList的Activity中添加:
android:theme="@android:style/Theme.Holo.Light"
- 创建数据库表地方添加颜色的字段:
@Override
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"
+ ");");
}
- 在系统中预定于好五种颜色,根据颜色对应不int值选择要显示的颜色,契约类中的定义:
public static final int DEFAULT_COLOR = 0;
public static final int YELLOW_COLOR = 1;
public static final int BLUE_COLOR = 2;
public static final int GREEN_COLOR = 3;
public static final int RED_COLOR = 4;
- 由于数据库中多了一个字段,所以要在NotePadProvider中添加对其相应的处理
static{}:
sNotesProjectionMap.put(
NotePad.Notes.COLUMN_NAME_BACK_COLOR,
NotePad.Notes.COLUMN_NAME_BACK_COLOR);
insert:
if (values.containsKey(NotePad.Notes.COLUMN_NAME_BACK_COLOR) == false) {
values.put(NotePad.Notes.COLUMN_NAME_BACK_COLOR, NotePad.Notes.DEFAULT_COLOR);
}
- 自定义一个CursorAdapter继承SimpleCursorAdapter,既能完成cursor读取的数据库内容填充到item,又能将颜色填充:
public class MyCursorAdapter extends SimpleCursorAdapter {
public MyCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
}
@Override
public void bindView(View view, Context context, Cursor cursor){
super.bindView(view, context, cursor);
int x = cursor.getInt(cursor.getColumnIndex(NotePad.Notes.COLUMN_NAME_BACK_COLOR));
switch (x){
case NotePad.Notes.DEFAULT_COLOR:
view.setBackgroundColor(Color.rgb(255, 255, 255));
break;
case NotePad.Notes.YELLOW_COLOR:
view.setBackgroundColor(Color.rgb(247, 216, 133));
break;
case NotePad.Notes.BLUE_COLOR:
view.setBackgroundColor(Color.rgb(165, 202, 237));
break;
case NotePad.Notes.GREEN_COLOR:
view.setBackgroundColor(Color.rgb(161, 214, 174));
break;
case NotePad.Notes.RED_COLOR:
view.setBackgroundColor(Color.rgb(244, 149, 133));
break;
default:
view.setBackgroundColor(Color.rgb(255, 255, 255));
break;
}
}
}
- NoteList中的PROJECTION添加颜色项:
private static final String[] PROJECTION = new String[] {
NotePad.Notes._ID,
NotePad.Notes.COLUMN_NAME_TITLE,
NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE,
NotePad.Notes.COLUMN_NAME_BACK_COLOR,
};
并且将NoteList中用的SimpleCursorAdapter改使用MyCursorAdapter:
adapter = new MyCursorAdapter(
this,
R.layout.noteslist_item,
cursor,
dataColumns,
viewIDs
);
作者:056 原文链接
|