android开发---记事本(一)
2015-08-03 00:00
295 查看
最近手机刷机了,原来我比较喜欢用的记事本刷没了,所以准备自己搞个笔记本小软件。
一、需求分析
主要功能就是用户记录事件、事件展示。功能比较简单,另外我不会美工,界面就不要求太高了,能满足我的使用就足够了。后续如果时间充足,再丰富一下功能。最近在学习xUtils,所以项目中会用到相关功能模块。
二、功能设计
废话不多说,开始敲代码。首先是导入xUtils包。然后是主界面布局。
首先在application中创建数据库。Note实体类,主要字段就是事件内容和创建时间。
主界面负责展示用户记录的事,在页面左下角和右下角分别添加一个按钮,用来删除和添加事件。将从数据库中读取数据放到 onResume()方法中,这样在从添加事件的界面返回时,可以实时加载数据。这里需要注意的一点是,在Note实体类中要有无参的构造方法,否则查找不到。
最后的任务就是在AddNoteActivity中实现添加事件的功能。
今天的工作就暂时做到这儿吧,我将在下一篇博客中完善这个小应用。
下一篇:记事本(二)
一、需求分析
主要功能就是用户记录事件、事件展示。功能比较简单,另外我不会美工,界面就不要求太高了,能满足我的使用就足够了。后续如果时间充足,再丰富一下功能。最近在学习xUtils,所以项目中会用到相关功能模块。
二、功能设计
废话不多说,开始敲代码。首先是导入xUtils包。然后是主界面布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ListView android:id="@+id/listview_main" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/layout_bottom" android:layout_alignParentTop="true" android:listSelector="@color/transparent" android:scrollbars="none" /> <include android:id="@+id/layout_bottom" layout="@layout/layout_main_bottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" /> </RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <TextView android:id="@+id/tv_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:maxLines="2" android:ellipsize="end" android:textSize="18sp" android:text="内容" /> <TextView android:id="@+id/tv_date" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:layout_marginBottom="10dp" android:maxLines="1" android:textSize="14sp" android:text="时间" android:gravity="end" /> </LinearLayout>
首先在application中创建数据库。Note实体类,主要字段就是事件内容和创建时间。
public class NotePadApp extends Application { @Override public void onCreate() { DbUtils dbUtils = DbUtils.create(this); try { dbUtils.createTableIfNotExist(Note.class); } catch (DbException e) { e.printStackTrace(); Toast.makeText(this, "创建数据库失败", Toast.LENGTH_SHORT).show(); } super.onCreate(); } }
主界面负责展示用户记录的事,在页面左下角和右下角分别添加一个按钮,用来删除和添加事件。将从数据库中读取数据放到 onResume()方法中,这样在从添加事件的界面返回时,可以实时加载数据。这里需要注意的一点是,在Note实体类中要有无参的构造方法,否则查找不到。
protected void onResume() { super.onResume(); notes.clear(); DbTask dbTask = new DbTask(); dbTask.execute(); }
private class DbTask extends AsyncTask<Void, Void, ArrayList<Note>> { @Override protected ArrayList<Note> doInBackground(Void... params) { DbUtils dbUtils = DbUtils.create(MainActivity.this); ArrayList<Note> notes = new ArrayList<Note>(); try { notes = (ArrayList<Note>) dbUtils.findAll(Note.class); } catch (DbException e) { e.printStackTrace(); Toast.makeText(MainActivity.this, "数据读取失败", Toast.LENGTH_SHORT).show(); } return notes; } @Override protected void onPostExecute(ArrayList<Note> ns) { notes = ns; mAdapter.notifyDataSetChanged(); super.onPostExecute(ns); } }
最后的任务就是在AddNoteActivity中实现添加事件的功能。
private boolean saveNote() { String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis())); Note note = new Note(edt_content.getText().toString(),date); DbUtils dbUtils = DbUtils.create(this); dbUtils.configDebug(true); try { dbUtils.save(note); ArrayList<Note> n = (ArrayList<Note>) dbUtils.findAll(Note.class); return true; } catch (DbException e) { e.printStackTrace(); Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show(); return false; } }
今天的工作就暂时做到这儿吧,我将在下一篇博客中完善这个小应用。
下一篇:记事本(二)
相关文章推荐
- Android实现表情 抓取新浪表情
- 详解Android解析Xml的三种方式——DOM、SAX以及XMLpull
- 可以买到 Linux 电脑的 10 个地方
- 天才从哪里来?(Where Does Talent Really Come From?)
- 重温80年前的美国
- 基督教笔记(六):阿弥士人(Amish)
- 美国1946年就业法(Employment Act of 1946)
- 美元(Dollar)
- 花生牛轧糖
- 权利法案(Bill of Rights)
- 基督教笔记(七):摩门教(Mormon)
- 伟大的报纸是什么样的?
- 中国畅销书的销量
- 川端康成《雪国》
- 伪装的"刀锋" - 阮一峰的网络日志
- 向秀《思旧赋》疏注
- 图形化简历
- 中国图书出版监管制度(续)
- 图书出版流程
- 引言摘录之一