Android手機(jī)開發(fā)設(shè)計(jì)之記事本功能
本文實(shí)例為大家分享了Android手機(jī)開發(fā)設(shè)計(jì)之記事本功能,供大家參考,具體內(nèi)容如下
一、需求分析
1.1業(yè)務(wù)需求分析
近年來,隨著生活節(jié)奏的加快,工作和生活的雙重壓力全面侵襲著人們,如何避免忘記工作和生活中的諸多事情而造成不良的后果就顯得非常重要。為此我們開發(fā)一款基于Android系統(tǒng)的簡單記事本,其能夠便攜記錄生活和工作對諸多事情,從而幫助人們有效地進(jìn)行時間管理。
1.2功能需求分析
本記事本項(xiàng)目希望可以開發(fā)出一款符合用戶生活工作習(xí)慣的簡單應(yīng)用,能夠滿足用戶的各方面需求,可以對記事進(jìn)行增加、查看、修改和刪除,要求功能完善豐富并且具有良好的用戶界面和交互體驗(yàn)。
二、項(xiàng)目設(shè)計(jì)
2.1功能模塊設(shè)計(jì)
2.1.1記事本基本操作
記事本基本操作是該項(xiàng)目的核心部分,提供添加、查看、修改、刪除記事本信息的功能,提供記事本動態(tài)更新的功能。
2.1.2 記事本主界面列表展示
用戶可能需要創(chuàng)建許多記事事項(xiàng),并且需要對這些記錄事項(xiàng)進(jìn)程基本操作,記事本的列表展示能使用戶界面更加簡潔清晰,且給用戶帶來使用方便。
2.1.3 記事本數(shù)據(jù)存儲
記事本最重要功能即記錄和保存用戶易遺忘的日期和事件,為了持久地將用戶記錄事項(xiàng)信息保存下來,需要將這些信息存儲到數(shù)據(jù)庫中,記事本需要保存的信息字段有編號、事件內(nèi)容和保存事件的具體時間。
其功能模塊圖如圖所示。
2.2數(shù)據(jù)庫設(shè)計(jì)
由上面的功能模塊分析可知,本記事本項(xiàng)目的數(shù)據(jù)庫設(shè)計(jì)主要包括三個字段名:編號id、事件內(nèi)容content和保存事件的時間notetime,其數(shù)據(jù)庫表如下表所示:
2.3界面設(shè)計(jì)
2.3.1記事本主界面
該界面主要包括添加按鈕和記錄事項(xiàng)列表。記事本主界面設(shè)計(jì)如下圖所示。
2.3.2 添加記錄事項(xiàng)界面
該界面主要包括清除內(nèi)容和保存內(nèi)容按鈕以及文本編輯。記事本添加記錄事項(xiàng)如下圖所示。
三、項(xiàng)目實(shí)現(xiàn)
3.1 NotepadBean類
由于記事本中的每個記錄都會有其唯一的編號id、記錄內(nèi)容notepadContent和保存記錄的時間notepadTime屬性,因此我們需要創(chuàng)建一個NotepadBean類用于存放這些屬性,并實(shí)現(xiàn)其相應(yīng)的getter和setter方法,其主要代碼如下:
public class NotepadBean { ? ? private String id;//記錄編號 ? ? private String notepadContent;//記錄的內(nèi)容 ? ? private String notepadTime;//保存記錄的時間 ? ? public String getId() { ? ? ? ? return id; ? ? } ? ? public void setId(String id) { ? ? ? ? this.id = id; ? ? } ? ? public String getNotepadContent() { ? ? ? ? return notepadContent; ? ? } ? ? public void setNotepadContent(String notepadContent) { ? ? ? ? this.notepadContent = notepadContent; ? ? } ? ? public String getNotepadTime() { ? ? ? ? return notepadTime; ? ? } ? ? public void setNotepadTime(String notepadTime) { ? ? ? ? this.notepadTime = notepadTime; ? ? } }
3.2 NotepadAdapter類
由于記事本界面的記錄列表是使用ListView控件展示,因此需要創(chuàng)建一個數(shù)據(jù)適配器NotepadAdapter類對ListView控件進(jìn)行數(shù)據(jù)適配,我們可以先創(chuàng)建NotepadAdapter類,再在NotepadAdapter類中創(chuàng)建一個ViewHolder類初始化Item界面中的控件,其中主要代碼如下:
public View getView(int position, View convertView, ViewGroup parent) { ? ? ViewHolder viewHolder; ? ? if (convertView==null){//加載Item界面對應(yīng)的布局文件 ? ? ? ? convertView=layoutInflater.inflate(R.layout.notepad_item,null); ? ? ? ? viewHolder=new ViewHolder(convertView);//創(chuàng)建ViewHolder對象 ? ? ? ? convertView.setTag(viewHolder);//創(chuàng)建ViewHolder對象 ? ? }else { ? ? ? ? viewHolder=(ViewHolder) convertView.getTag();//convertView關(guān)聯(lián)ViewHolder對象 ? ? } ? ? NotepadBean notepadBean=(NotepadBean)getItem(position);//將獲取的數(shù)據(jù)顯示到對應(yīng)的控件上 ? ? viewHolder.tvNotepadContent.setText(notepadBean.getNotepadContent()); ? ? viewHolder.tvNotepadTime.setText(notepadBean.getNotepadTime()); ? ? return convertView; } class ViewHolder{ ? ? TextView tvNotepadContent; ? ? TextView tvNotepadTime; ? ? public ViewHolder(View view){ ? ? ? ? tvNotepadContent=view.findViewById(R.id.item_content);//記錄的內(nèi)容 ? ? ? ? tvNotepadTime=view.findViewById(R.id.item_time);//保存記錄的時間 ? ? } }
3.3 SQLiteHelper類
在記事本程序中存儲和讀取記錄的數(shù)據(jù)都是通過操作數(shù)據(jù)庫完成的,我們需要創(chuàng)建SQLiteHelper類實(shí)現(xiàn)對數(shù)據(jù)庫中表的增刪改查,以及利用數(shù)據(jù)庫中的工具類DBUtils來定義數(shù)據(jù)庫的名稱、表名、數(shù)據(jù)庫版本、數(shù)據(jù)庫表中的列名以及獲取當(dāng)前日期等信息,其主要代碼如下:
創(chuàng)建數(shù)據(jù)庫:
public SQLiteHelper(Context context){ ? ? super(context, DBUtils.DATABASE_NAME,null,DBUtils.DATABASE_VERION); ? ? sqLiteDatabase=this.getWritableDatabase(); }
創(chuàng)建表:
@Override public void onCreate(SQLiteDatabase db){ ? ? db.execSQL("CREATE TABLE "+DBUtils.DATABASE_TABLE+"("+DBUtils.NOTEPAD_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+DBUtils.NOTEPAD_CONTENT+" text, "+DBUtils.NOTEPAD_TIME+" text)"); }
添加數(shù)據(jù):
public boolean insertData(String userContent,String userTime){ ? ? ContentValues contentValues=new ContentValues(); ? ? contentValues.put(DBUtils.NOTEPAD_CONTENT,userContent); ? ? contentValues.put(DBUtils.NOTEPAD_TIME,userTime); ? ? return sqLiteDatabase.insert(DBUtils.DATABASE_TABLE,null,contentValues)>0; }
刪除數(shù)據(jù):
public boolean deleteData(String id){ ? ? String sql=DBUtils.NOTEPAD_ID+"=?"; ? ? String[] contentValuesArray=new String[]{String.valueOf(id)}; ? ? return sqLiteDatabase.delete(DBUtils.DATABASE_TABLE,sql,contentValuesArray)>0; }
修改數(shù)據(jù):
public boolean updateData(String id,String content,String userYear){ ? ? ContentValues contentValues=new ContentValues(); ? ? contentValues.put(DBUtils.NOTEPAD_CONTENT,content); ? ? contentValues.put(DBUtils.NOTEPAD_TIME,userYear); ? ? String sql=DBUtils.NOTEPAD_ID+"=?"; ? ? String[] strings=new String[]{id}; ? ? return sqLiteDatabase.update(DBUtils.DATABASE_TABLE,contentValues,sql,strings)>0; }
查詢數(shù)據(jù):
public List<NotepadBean> query(){ ? ? List<NotepadBean>list=new ArrayList<NotepadBean>(); ? ? Cursor cursor=sqLiteDatabase.query(DBUtils.DATABASE_TABLE,null,null, ? ? ? ? ? ? null,null,null,DBUtils.NOTEPAD_ID+" desc"); ? ? if (cursor!=null){ ? ? ? ? while (cursor.moveToNext()){ ? ? ? ? ? ? NotepadBean noteInfo=new NotepadBean(); ? ? ? ? ? ? String id=String.valueOf(cursor.getInt(cursor.getColumnIndex(DBUtils.NOTEPAD_ID))); ? ? ? ? ? ? String content=cursor.getString(cursor.getColumnIndex(DBUtils.NOTEPAD_CONTENT)); ? ? ? ? ? ? String time=cursor.getString(cursor.getColumnIndex(DBUtils.NOTEPAD_TIME)); ? ? ? ? ? ? noteInfo.setId(id); ? ? ? ? ? ? noteInfo.setNotepadContent(content); ? ? ? ? ? ? noteInfo.setNotepadTime(time); ? ? ? ? ? ? list.add(noteInfo); ? ? ? ? } ? ? ? ? cursor.close(); ? ? } ? ? return list; }
3.4 NotepadActivity類
記事本主界面包含顯示列表和添加按鈕功能,我們創(chuàng)建NotepadActivity類實(shí)現(xiàn),其中顯示列表在NotepadActivity類中通過創(chuàng)建showQueryData()方法,在該方法中查詢數(shù)據(jù)庫存放的記錄信息,并將該信息顯示到記錄列表中,其實(shí)現(xiàn)代碼如下:
private void showQueryData(){ ? ? if(list!=null){ ? ? ? ? list.clear(); ? ? } ? ? list=mSQLiteHelper.query(); ? ? adapter=new NotepadAdapter(this,list); ? ? listView.setAdapter(adapter); }
為“添加按鈕”通過setOnClickListener()方法設(shè)置點(diǎn)擊事件,當(dāng)點(diǎn)擊該按鈕時,跳轉(zhuǎn)到添加記錄的界面,其實(shí)現(xiàn)代碼如下:
protected void onCreate(Bundle savedInstanceState) { ? ? super.onCreate(savedInstanceState); ? ? setContentView(R.layout.activity_main); ? ? listView=findViewById(R.id.listview); ? ? ImageView imageView=findViewById(R.id.add); ? ? initData(); ? ? imageView.setOnClickListener(new View.OnClickListener() { ? ? ? ? @Override ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? Intent intent=new Intent(NotepadActivity.this,RecordActivity.class); ? ? ? ? ? ? startActivityForResult(intent,1); ? ? ? ? } ? ? }); }
3.5 RecordActivity類
RecordActivity為修改記錄,我們在NotepadActivity中通過listView的setOnItemClickListener()方法監(jiān)聽Item的點(diǎn)擊事件,攜帶被點(diǎn)擊Item的記錄內(nèi)容跳轉(zhuǎn)到RecordActivity中,其會根據(jù)獲取的數(shù)據(jù)顯示記錄的內(nèi)容。另外當(dāng)我們需要刪除記事本列表中的記錄時,需要長按列表中的Item,此時會彈出一個對話框提示是否刪除Item對應(yīng)的事件。setOnItemClickListener()方法代碼如下:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){ ? ? ? ? @Override ? ? ? ? public void onItemClick(AdapterView<?>parent,View view,int position,long id ){ ? ? ? ? ? ? NotepadBean notepadBean=list.get(position); ? ? ? ? ? ? Intent intent=new Intent(NotepadActivity.this,RecordActivity.class); ? ? ? ? ? ? intent.putExtra("id",notepadBean.getId()); ? ? ? ? ? ? intent.putExtra("content",notepadBean.getNotepadContent()); ? ? ? ? ? ? intent.putExtra("time",notepadBean.getNotepadTime()); ? ? ? ? ? ? NotepadActivity.this.startActivityForResult(intent,1); ? ? ? ? } ? ? }); ? ? listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { ? ? ? ? @Override ? ? ? ? public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) { ? ? ? ? ? ? AlertDialog dialog; ? ? ? ? ? ? AlertDialog.Builder builder=new AlertDialog.Builder(NotepadActivity.this) ? ? ? ? ? ? ? ? ? ? .setMessage("是否刪除此記錄?") ? ? ? ? ? ? ? ? ? ? .setPositiveButton("確定", new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? NotepadBean notepadBean=list.get(position); ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(mSQLiteHelper.deleteData(notepadBean.getId())){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? list.remove(position);//刪除對應(yīng)的Item ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? adapter.notifyDataSetChanged();//更新記事本頁面 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(NotepadActivity.this,"刪除成功",Toast.LENGTH_LONG).show(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? ? ? .setNegativeButton("取消", new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? dialog.dismiss(); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? dialog=builder.create(); ? ? ? ? ? ? dialog.show(); ? ? ? ? ? ? return true; ? ? ? ? } });
RecordActivity利用initData()函數(shù)接收傳遞的數(shù)據(jù)去,其代碼如下:
public void initData(){ ? ? mSQLiteHelper=new SQLiteHelper(this); ? ? noteName.setText("添加記錄"); ? ? Intent intent=getIntent(); ? ? if(intent!=null){ ? ? ? ? id=intent.getStringExtra("id"); ? ? ? ? if(id!=null){ ? ? ? ? ? ? noteName.setText("修改記錄"); ? ? ? ? ? ? content.setText(intent.getStringExtra("content")); ? ? ? ? ? ? note_time.setText(intent.getStringExtra("time")); ? ? ? ? ? ? note_time.setVisibility(View.VISIBLE); ? ? ? ? } ? ? } }
同時,我們在RecordActivity中可以利用switch…case結(jié)構(gòu)實(shí)現(xiàn)了編輯記錄、保存和清除編輯的記錄的功能,即通過EditText控件實(shí)現(xiàn)記錄的編輯功能,為保存按鈕設(shè)置點(diǎn)擊事件;當(dāng)點(diǎn)擊保存按鈕時將記錄的內(nèi)容和保存時間通過SQLiteHelper類的insertData()方法添加到數(shù)據(jù)庫中;為清除按鈕設(shè)置點(diǎn)擊事件,當(dāng)點(diǎn)擊清除按鈕時,將EditText控件的內(nèi)容通過setText()方法置為空字符串。其OnClick()函數(shù)代碼如下:
public void onClick(View v){ ? ? switch (v.getId()){ ? ? ? ? case R.id.note_back: ? ? ? ? ? ? finish(); ? ? ? ? ? ? break; ? ? ? ? case R.id.delete: ? ? ? ? ? ? content.setText(" "); ? ? ? ? ? ? break; ? ? ? ? case R.id.note_save: ? ? ? ? ? ? String noteContent =content.getText().toString().trim(); ? ? ? ? ? ? if(id!=null){ ? ? ? ? ? ? ? ? //修改記錄的功能 ? ? ? ? ? ? ? ? if(noteContent.length()>0){ ? ? ? ? ? ? ? ? ? ? if (mSQLiteHelper.updateData(id,noteContent,DBUtils.getTime())){ ? ? ? ? ? ? ? ? ? ? ? ? showToast("修改成功"); ? ? ? ? ? ? ? ? ? ? ? ? setResult(2); ? ? ? ? ? ? ? ? ? ? ? ? finish(); ? ? ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? ? ? showToast("修改失敗"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } else{ ? ? ? ? ? ? ? ? ? ? showToast("修改的記錄內(nèi)容不能為空"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? //添加記錄的功能 ? ? ? ? ? ? ? ? if(noteContent.length()>0){ ? ? ? ? ? ? ? ? ? ? if (mSQLiteHelper.insertData(noteContent,DBUtils.getTime())){ ? ? ? ? ? ? ? ? ? ? ? ? showToast("保存成功"); ? ? ? ? ? ? ? ? ? ? ? ? setResult(2); ? ? ? ? ? ? ? ? ? ? ? ? finish(); ? ? ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? ? ? showToast("保存失敗"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } else{ ? ? ? ? ? ? ? ? ? ? showToast("保存的記錄內(nèi)容不能為空"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? } }
四、項(xiàng)目測試
1、項(xiàng)目運(yùn)行主界面。
2、點(diǎn)擊主界面添加進(jìn)入添加頁面,輸入“Android課程設(shè)計(jì)”然后點(diǎn)擊保存按鈕會返回主界面并彈出“保存成功”信息。
3、長按我們剛剛新建的記錄“Android課程設(shè)計(jì)”會彈出刪除對話框,點(diǎn)擊確定即可刪除,并彈出“刪除成功”信息。
4、選擇并打開“20182800”記錄,將其修改為“20180000”,然后點(diǎn)擊保存按鈕,會彈出“修改成功”的信息。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)簡易記事本
- Android實(shí)現(xiàn)記事本小功能
- Android記事本項(xiàng)目開發(fā)
- Android實(shí)現(xiàn)記事本功能
- Android實(shí)現(xiàn)簡易記事本
- android實(shí)現(xiàn)記事本app
- Android+SQLite數(shù)據(jù)庫實(shí)現(xiàn)的生詞記事本功能實(shí)例
- Android中實(shí)現(xiàn)記事本動態(tài)添加行效果
- Android實(shí)現(xiàn)記事本功能(26)
- Android利用Intent實(shí)現(xiàn)記事本功能(NotePad)
相關(guān)文章
Android studio 混淆+打包+驗(yàn)證是否成功
本文主要介紹了Android studio 混淆+打包+驗(yàn)證是否成功的相關(guān)知識,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03Flutter通過Container實(shí)現(xiàn)時間軸效果
時間軸是前端UI經(jīng)常用到的效果,本文講解下Flutter如何通過Container實(shí)現(xiàn),感興趣的朋友可以了解下2021-05-05Android 自定義彈性ListView控件實(shí)例代碼(三種方法)
關(guān)于在Android中實(shí)現(xiàn)ListView的彈性效果,有很多不同的方法,網(wǎng)上一搜,也有很多,下面貼出在項(xiàng)目中經(jīng)常用到的兩種實(shí)現(xiàn)ListView彈性效果的方法(基本上拿來就可以用),需要的朋友參考下本段代碼2016-01-01Android實(shí)現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單(1)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單第一篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android Gradle多渠道打包的實(shí)現(xiàn)方法
這篇文章主要介紹了Android Gradle多渠道打包的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11flutter實(shí)現(xiàn)底部不規(guī)則導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)底部不規(guī)則導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07