android开发---记事本(一)

最近手机刷机了,原来我比较喜欢用的记事本刷没了,所以准备自己搞个笔记本小软件。

一、需求分析

主要功能就是用户记录事件、事件展示。功能比较简单,另外我不会美工,界面就不要求太高了,能满足我的使用就足够了。后续如果时间充足,再丰富一下功能。最近在学习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;
        }
    }

今天的工作就暂时做到这儿吧,我将在下一篇博客中完善这个小应用。
下一篇:记事本(二)

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值