简单的安卓记事本

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/belove_lucy/article/details/54289637

在Android系统的手机开发平台之上,研究与开发一个记事本软件,提供适用手机的记事本功能服务。

流程图:
这里写图片描述

数据库设计实现:

        该系统所有数据存在数据库中,本次实验采用SQLite来对数据库进行操作。
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中。它具有零配置、足够小、操作速度快等优点,被广泛应用于手机应用软件中。
        新建DatabaseHelper类作为数据库辅助类,继承SQLiteOpenHelper类。在DatabaseHelper类的构造方法中创建数据库,并在onCreate上创建表及属性。

package cn.edu.nbut.mytextbook;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by ~polm on 2016/12/14.
 */
public class DatabaseHelper extends SQLiteOpenHelper {
    static String name="book.db";
    static int version=1;
    public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public void onCreate(SQLiteDatabase db) {
        String sql="create table mytext(id integer primary key autoincrement,title text not null, content text,time datetime not null default getDate())";
        db.execSQL(sql);
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

模块功能+界面设计+Activity实现代码

封面:
        封面主要是用户在打开页面是展示出来的一张图片,有一个良好的过渡作用。界面就是由一张有关于记事本的图片所构成,该图片填充与整个界面,为了美观将界面的标题栏隐藏。该界面停留三秒自动转入到主页面,时间不长,不会让用户感到等待的烦躁。
这里写图片描述

package cn.edu.nbut.mytextbook;

import android.content.Intent;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Handler;

public class FirstActivity extends AppCompatActivity {

    final Timer timer = new Timer();
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getSupportActionBar().hide();
        setContentView(R.layout.activity_first);

        imageView = (ImageView)findViewById(R.id.imageView);

        timerinit();

    }

    private void timerinit() {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                Intent intent = new Intent(FirstActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        };timer.schedule(task, 3000);
    }

    public boolean OnKeyDown(int keyCode, KeyEvent event) {
        timer.cancel();
        return super.onKeyDown(keyCode, event);
    }
}

当天记事内容:
        当天记事内容主要用于向用户展示用户当天所添加或更改的记事。该模块方便用户查看自己的最新动态,记事内容将按时间顺序在页面上排列,第一行数据则是用户最新添加或更改的记事。实现方法:用户界面防止一个listview控件,该控件向数据库中读取数据,设两个数组,一个存放整型,一个存放字符型。整型数组存放的是一条记事的id,字符型数组存放的是title和time。字符型数组用于展示在页面上,整型数组用于listview适配器上,再对listview中的每一条item添加触发事件。
这里写图片描述

package cn.edu.nbut.mytextbook;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private static final int NEWBACK = 3;
    private static final int NEWBACK2 = 3;
    final static int Menu_New = Menu.FIRST;
    final static int Menu_Before = Menu.FIRST+1;
    final static int Menu_Exit = Menu.FIRST+2;
    Menu menu;
    ArrayList<Integer> IdArray = new ArrayList<Integer>();

    class itemListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Intent intent = new Intent(MainActivity.this, UpdateActivity.class);
            intent.putExtra("id", IdArray.get(arg2));
            startActivityForResult(intent, NEWBACK2);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView)findViewById(R.id.listView);
        InitData();
        listView.setOnItemClickListener(new itemListener());
    }

    public void InitData() {
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String timenow = format.format(timestamp);
        DatabaseHelper helper = new DatabaseHelper(MainActivity.this, "book.db", null, 1);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cs = db.query("mytext", new String[]{"id", "title", "time"}, null, null, null, null, "time desc");
        ArrayList<String> al = new ArrayList<String>();
        IdArray.clear();
        while(cs.moveToNext()) {
            int id = cs.getInt(cs.getColumnIndex("id"));
            String title = cs.getString(cs.getColumnIndex("title"));
            String time = cs.getString(cs.getColumnIndex("time"));
            if (time.compareTo(timenow) >= 0) {
                IdArray.add(id);
                al.add(title + "\n" + time);
            }
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, al);
        listView.setAdapter(adapter);
    }

    @Override
    public  boolean onCreateOptionsMenu (Menu menu) {
        menu.add(0, Menu_New, 0, "添加");
        menu.add(0, Menu_Before, 0, "历史记录");
        menu.add(0, Menu_Exit, 0, "退出");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected (MenuItem item) {
        switch (item.getItemId()) {
            case Menu_New:
                Intent intent = new Intent(MainActivity.this, NewActivity.class);
                startActivityForResult(intent, NEWBACK);
                break;
            case Menu_Before:
                Intent intent1 = new Intent(MainActivity.this, BeforeActivity.class);
                startActivity(intent1);
                MainActivity.this.finish();
                  break;
            case Menu_Exit:
                System.exit(0);
        }
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        InitData();

        Toast.makeText(MainActivity.this, "刷新成功", Toast.LENGTH_LONG).show();
    }
}

历史记事内容:
        历史记事内容用来向用户展示用户非今天所添加修改的记事。将历史与当天设成两个模块,防止界面展示太多的记事内容,使用户翻看困难。该模块实现功能与当天记事内容相同,区别在于两者向数据库读取数据所限制的时间范围不同。时间的比较采用字符串比较,获取当前时间与记事时间相比较。
这里写图片描述

package cn.edu.nbut.mytextbook;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class BeforeActivity extends AppCompatActivity {

    private ListView listView;
    private static final int NEWBACK2 = 3;
    final static int Menu_Back = Menu.FIRST;
    final static int Menu_Exit = Menu.FIRST+1;
    Menu menu;
    ArrayList<Integer> IdArray = new ArrayList<Integer>();

    class itemListener2 implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Intent intent = new Intent(BeforeActivity.this, UpdateActivity.class);
            intent.putExtra("id", IdArray.get(arg2));
            startActivityForResult(intent, NEWBACK2);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_before);

        listView = (ListView)findViewById(R.id.listView2);
        InitData();
        listView.setOnItemClickListener(new itemListener2());
    }

    public void InitData() {
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String timenow = format.format(timestamp);
        DatabaseHelper helper = new DatabaseHelper(BeforeActivity.this, "book.db", null, 1);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cs = db.query("mytext", new String[]{"id", "title", "time"}, null, null, null, null, "time desc");
        ArrayList<String> al = new ArrayList<String>();
        IdArray.clear();
        while(cs.moveToNext()) {
            int id = cs.getInt(cs.getColumnIndex("id"));
            String title = cs.getString(cs.getColumnIndex("title"));
            String time = cs.getString(cs.getColumnIndex("time"));
            if (time.compareTo(timenow) >= 0) {}
            else {
                IdArray.add(id);
                al.add(title + "\n" + time);
            }
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, al);
        listView.setAdapter(adapter);
    }

    @Override
    public  boolean onCreateOptionsMenu (Menu menu) {
        menu.add(0, Menu_Back, 0, "返回");
        menu.add(0, Menu_Exit, 0, "退出");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected (MenuItem item) {
        switch (item.getItemId()) {
            case Menu_Back:
                Intent intent = new Intent(BeforeActivity.this, MainActivity.class);
                startActivity(intent);
                BeforeActivity.this.finish();
                break;
            case Menu_Exit:
                System.exit(0);
        }
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        InitData();

        Toast.makeText(BeforeActivity.this, "刷新成功", Toast.LENGTH_LONG).show();
    }
}

添加新记事:
        添加新记事主要是完成用户添加新记事这项功能。该界面上有两个textview、两个edittext和一个button。textview用于提示用户该项所要填写的用户,edittext用于用户填写内容,button用于实现将用户填写内容添加到数据库的功能。button实现原理:先获取用户填写的标题和内容,再实现系统当前时间。将要传入数据库的内容打包在ContentValues的变量value中,利用SQLiteDatabase中的insert函数将value加入到数据库中。
这里写图片描述

package cn.edu.nbut.mytextbook;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class NewActivity extends AppCompatActivity {

    EditText editTitle, editContent;
    Button fini;

    class finishListener implements View.OnClickListener{
        @Override
        public void onClick(View arg0) {
            String title = editTitle.getText().toString();
            String content = editContent.getText().toString();
            Date date = new Date();
            Timestamp timestamp = new Timestamp(date.getTime());
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = format.format(timestamp);
            DatabaseHelper help = new DatabaseHelper(NewActivity.this, "book.db", null, 1);
            SQLiteDatabase db = help.getWritableDatabase();
            ContentValues value = new ContentValues();
            value.put("title", title);
            value.put("content", content);
            value.put("time", time);
            db.insert("mytext", null, value);

            Toast.makeText(NewActivity.this, "添加成功", Toast.LENGTH_LONG).show();

            NewActivity.this.setResult(RESULT_OK, getIntent());
            NewActivity.this.finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);

        editTitle = (EditText)findViewById(R.id.editTitle);
        editContent = (EditText)findViewById(R.id.editContent);
        fini = (Button)findViewById(R.id.but_finish);

        final Intent intent = this.getIntent();

        fini.setOnClickListener(new finishListener());
    }
}

修改记事:
        修改记事主要用于用户修改已添加的的记事。界面上放置两个textview、两个edittext和两个button。textview用于提示用户该项所要填写的用户,edittext用于填充从数据库中读取的数据,button分别完成删除和保存修改的功能。数据填充利用SQLiteDatebase中的query来完成数据从数据库中读取,然后再分别填充。记事的修改保存类似于完成添加新记事,但是将利用update函数而不是insert了。删除利用SQLiteDatabase中的delete函数,根据id关键字进行删除。
这里写图片描述

package cn.edu.nbut.mytextbook;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UpdateActivity extends AppCompatActivity {

    public int id;
    EditText editTitle, editContent;
    Button chan, dele;

    class changeListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Date date = new Date();
            Timestamp timestamp = new Timestamp(date.getTime());
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String timenow = format.format(timestamp);
            DatabaseHelper helper = new DatabaseHelper(UpdateActivity.this, "book.db", null, 1);
            SQLiteDatabase db = helper.getWritableDatabase();
            ContentValues value = new ContentValues();
            String title = editTitle.getText().toString();
            String content = editContent.getText().toString();
            value.put("title", title);
            value.put("content", content);
            value.put("time", timenow);
            db.update("mytext", value, "id=?", new String[]{id+""});
            db.close();

            Toast.makeText(UpdateActivity.this, "更新成功", Toast.LENGTH_LONG).show();

            UpdateActivity.this.setResult(RESULT_OK, getIntent());
            UpdateActivity.this.finish();
        }
    }

    class deleteListener implements View.OnClickListener{
        @Override
        public void onClick(View arg0) {
            DatabaseHelper helper = new DatabaseHelper(UpdateActivity.this, "book.db", null, 1);
            SQLiteDatabase db = helper.getWritableDatabase();
            db.delete("mytext", "id=?", new String[]{id+""});
            db.close();
            editTitle.setText("");
            editContent.setText("");

            Toast.makeText(UpdateActivity.this, "删除成功", Toast.LENGTH_LONG).show();

            UpdateActivity.this.setResult(RESULT_OK, getIntent());
            UpdateActivity.this.finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);

        editTitle = (EditText)findViewById(R.id.etTitle);
        editContent = (EditText)findViewById(R.id.etContent);
        chan = (Button)findViewById(R.id.but_change);
        dele = (Button)findViewById(R.id.but_delete);

        Intent intent = this.getIntent();
        id = intent.getIntExtra("id", 1);
        fillData();

        chan.setOnClickListener(new changeListener());
        dele.setOnClickListener(new deleteListener());
    }

    private void fillData() {
        DatabaseHelper helper = new DatabaseHelper(UpdateActivity.this, "book.db", null, 1);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cs = db.query("mytext", new String[]{"id", "title", "content"}, "id=?", new String[]{id+""}, null, null, null);
        while(cs.moveToNext()) {
            String title = cs.getString(cs.getColumnIndex("title"));
            String content = cs.getString(cs.getColumnIndex("content"));
            editTitle.setText(title);
            editContent.setText(content);
        }
        db.close();
    }
}

以上就是整个代码实现了,只实现了基础功能,能力还不足,大家请多多指教^ ^

展开阅读全文

没有更多推荐了,返回首页