Android手機(jī)開(kāi)發(fā)設(shè)計(jì)之記事本功能
本文實(shí)例為大家分享了Android手機(jī)開(kāi)發(fā)設(shè)計(jì)之記事本功能,供大家參考,具體內(nèi)容如下
一、需求分析
1.1業(yè)務(wù)需求分析
近年來(lái),隨著生活節(jié)奏的加快,工作和生活的雙重壓力全面侵襲著人們,如何避免忘記工作和生活中的諸多事情而造成不良的后果就顯得非常重要。為此我們開(kāi)發(fā)一款基于Android系統(tǒng)的簡(jiǎn)單記事本,其能夠便攜記錄生活和工作對(duì)諸多事情,從而幫助人們有效地進(jìn)行時(shí)間管理。
1.2功能需求分析
本記事本項(xiàng)目希望可以開(kāi)發(fā)出一款符合用戶生活工作習(xí)慣的簡(jiǎn)單應(yīng)用,能夠滿足用戶的各方面需求,可以對(duì)記事進(jìn)行增加、查看、修改和刪除,要求功能完善豐富并且具有良好的用戶界面和交互體驗(yàn)。
二、項(xiàng)目設(shè)計(jì)
2.1功能模塊設(shè)計(jì)
2.1.1記事本基本操作
記事本基本操作是該項(xiàng)目的核心部分,提供添加、查看、修改、刪除記事本信息的功能,提供記事本動(dòng)態(tài)更新的功能。
2.1.2 記事本主界面列表展示
用戶可能需要?jiǎng)?chuàng)建許多記事事項(xiàng),并且需要對(duì)這些記錄事項(xiàng)進(jìn)程基本操作,記事本的列表展示能使用戶界面更加簡(jiǎn)潔清晰,且給用戶帶來(lái)使用方便。
2.1.3 記事本數(shù)據(jù)存儲(chǔ)
記事本最重要功能即記錄和保存用戶易遺忘的日期和事件,為了持久地將用戶記錄事項(xiàng)信息保存下來(lái),需要將這些信息存儲(chǔ)到數(shù)據(jù)庫(kù)中,記事本需要保存的信息字段有編號(hào)、事件內(nèi)容和保存事件的具體時(shí)間。
其功能模塊圖如圖所示。

2.2數(shù)據(jù)庫(kù)設(shè)計(jì)
由上面的功能模塊分析可知,本記事本項(xiàng)目的數(shù)據(jù)庫(kù)設(shè)計(jì)主要包括三個(gè)字段名:編號(hào)id、事件內(nèi)容content和保存事件的時(shí)間notetime,其數(shù)據(jù)庫(kù)表如下表所示:

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類
由于記事本中的每個(gè)記錄都會(huì)有其唯一的編號(hào)id、記錄內(nèi)容notepadContent和保存記錄的時(shí)間notepadTime屬性,因此我們需要?jiǎng)?chuàng)建一個(gè)NotepadBean類用于存放這些屬性,并實(shí)現(xiàn)其相應(yīng)的getter和setter方法,其主要代碼如下:
public class NotepadBean {
? ? private String id;//記錄編號(hào)
? ? private String notepadContent;//記錄的內(nèi)容
? ? private String notepadTime;//保存記錄的時(shí)間
? ? 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控件展示,因此需要?jiǎng)?chuàng)建一個(gè)數(shù)據(jù)適配器NotepadAdapter類對(duì)ListView控件進(jìn)行數(shù)據(jù)適配,我們可以先創(chuàng)建NotepadAdapter類,再在NotepadAdapter類中創(chuàng)建一個(gè)ViewHolder類初始化Item界面中的控件,其中主要代碼如下:
public View getView(int position, View convertView, ViewGroup parent) {
? ? ViewHolder viewHolder;
? ? if (convertView==null){//加載Item界面對(duì)應(yīng)的布局文件
? ? ? ? convertView=layoutInflater.inflate(R.layout.notepad_item,null);
? ? ? ? viewHolder=new ViewHolder(convertView);//創(chuàng)建ViewHolder對(duì)象
? ? ? ? convertView.setTag(viewHolder);//創(chuàng)建ViewHolder對(duì)象
? ? }else {
? ? ? ? viewHolder=(ViewHolder) convertView.getTag();//convertView關(guān)聯(lián)ViewHolder對(duì)象
? ? }
? ? NotepadBean notepadBean=(NotepadBean)getItem(position);//將獲取的數(shù)據(jù)顯示到對(duì)應(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);//保存記錄的時(shí)間
? ? }
}3.3 SQLiteHelper類
在記事本程序中存儲(chǔ)和讀取記錄的數(shù)據(jù)都是通過(guò)操作數(shù)據(jù)庫(kù)完成的,我們需要?jiǎng)?chuàng)建SQLiteHelper類實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)中表的增刪改查,以及利用數(shù)據(jù)庫(kù)中的工具類DBUtils來(lái)定義數(shù)據(jù)庫(kù)的名稱、表名、數(shù)據(jù)庫(kù)版本、數(shù)據(jù)庫(kù)表中的列名以及獲取當(dāng)前日期等信息,其主要代碼如下:
創(chuàng)建數(shù)據(jù)庫(kù):
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類中通過(guò)創(chuàng)建showQueryData()方法,在該方法中查詢數(shù)據(jù)庫(kù)存放的記錄信息,并將該信息顯示到記錄列表中,其實(shí)現(xiàn)代碼如下:
private void showQueryData(){
? ? if(list!=null){
? ? ? ? list.clear();
? ? }
? ? list=mSQLiteHelper.query();
? ? adapter=new NotepadAdapter(this,list);
? ? listView.setAdapter(adapter);
}為“添加按鈕”通過(guò)setOnClickListener()方法設(shè)置點(diǎn)擊事件,當(dāng)點(diǎn)擊該按鈕時(shí),跳轉(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為修改記錄,我們?cè)贜otepadActivity中通過(guò)listView的setOnItemClickListener()方法監(jiān)聽(tīng)I(yíng)tem的點(diǎn)擊事件,攜帶被點(diǎn)擊Item的記錄內(nèi)容跳轉(zhuǎn)到RecordActivity中,其會(huì)根據(jù)獲取的數(shù)據(jù)顯示記錄的內(nèi)容。另外當(dāng)我們需要?jiǎng)h除記事本列表中的記錄時(shí),需要長(zhǎng)按列表中的Item,此時(shí)會(huì)彈出一個(gè)對(duì)話框提示是否刪除Item對(duì)應(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);//刪除對(duì)應(yīng)的Item
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? adapter.notifyDataSetChanged();//更新記事本頁(yè)面
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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);
? ? ? ? }
? ? }
}同時(shí),我們?cè)赗ecordActivity中可以利用switch…case結(jié)構(gòu)實(shí)現(xiàn)了編輯記錄、保存和清除編輯的記錄的功能,即通過(guò)EditText控件實(shí)現(xiàn)記錄的編輯功能,為保存按鈕設(shè)置點(diǎn)擊事件;當(dāng)點(diǎn)擊保存按鈕時(shí)將記錄的內(nèi)容和保存時(shí)間通過(guò)SQLiteHelper類的insertData()方法添加到數(shù)據(jù)庫(kù)中;為清除按鈕設(shè)置點(diǎn)擊事件,當(dāng)點(diǎn)擊清除按鈕時(shí),將EditText控件的內(nèi)容通過(guò)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)目測(cè)試
1、項(xiàng)目運(yùn)行主界面。

2、點(diǎn)擊主界面添加進(jìn)入添加頁(yè)面,輸入“Android課程設(shè)計(jì)”然后點(diǎn)擊保存按鈕會(huì)返回主界面并彈出“保存成功”信息。

3、長(zhǎng)按我們剛剛新建的記錄“Android課程設(shè)計(jì)”會(huì)彈出刪除對(duì)話框,點(diǎn)擊確定即可刪除,并彈出“刪除成功”信息。

4、選擇并打開(kāi)“20182800”記錄,將其修改為“20180000”,然后點(diǎn)擊保存按鈕,會(huì)彈出“修改成功”的信息。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)簡(jiǎn)易記事本
- Android實(shí)現(xiàn)記事本小功能
- Android記事本項(xiàng)目開(kāi)發(fā)
- Android實(shí)現(xiàn)記事本功能
- Android實(shí)現(xiàn)簡(jiǎn)易記事本
- android實(shí)現(xiàn)記事本app
- Android+SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)的生詞記事本功能實(shí)例
- Android中實(shí)現(xiàn)記事本動(dòng)態(tài)添加行效果
- Android實(shí)現(xiàn)記事本功能(26)
- Android利用Intent實(shí)現(xiàn)記事本功能(NotePad)
相關(guān)文章
Android高手進(jìn)階教程(二十二)之Android中幾種圖像特效處理的集錦匯總!!
本篇文章主要介紹了Android中幾種圖像特效處理,比如圓角,倒影,還有就是圖片縮放,Drawable轉(zhuǎn)化為Bitmap,Bitmap轉(zhuǎn)化為Drawable等,有需要的可以了解一下。2016-11-11
AndroidStudio項(xiàng)目打包成jar的簡(jiǎn)單方法
JAR(Java Archive,Java 歸檔文件)是與平臺(tái)無(wú)關(guān)的文件格式,它允許將許多文件組合成一個(gè)壓縮文件,在eclipse中我們知道如何將一個(gè)項(xiàng)目導(dǎo)出為jar包,供其它項(xiàng)目使用呢?下面通過(guò)本文給大家介紹ndroidStudio項(xiàng)目打包成jar的簡(jiǎn)單方法,需要的朋友參考下吧2017-11-11
Kotlin開(kāi)發(fā)的一些實(shí)用小技巧總結(jié)
Kotlin 是一個(gè)基于 JVM 的新編程語(yǔ)言,用 JetBrains 的話來(lái)說(shuō)是「更現(xiàn)代化、更強(qiáng)大,所以下面這篇文章主要給大家總結(jié)介紹了關(guān)于Kotlin的一些開(kāi)發(fā)實(shí)用小技巧,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-10-10
Android開(kāi)發(fā)中PopupWindow用法實(shí)例分析
這篇文章主要介紹了Android開(kāi)發(fā)中PopupWindow用法,結(jié)合實(shí)例形式分析了PopupWindow彈出窗口效果的使用技巧,需要的朋友可以參考下2016-02-02
在Android系統(tǒng)源碼中預(yù)置APK的方法
今天小編就為大家分享一篇關(guān)于在Android系統(tǒng)源碼中預(yù)置APK的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Android實(shí)現(xiàn)橫向滑動(dòng)卡片效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)橫向滑動(dòng)卡片效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
5種方法完美解決android軟鍵盤(pán)擋住輸入框方法詳解
我們?cè)陂_(kāi)發(fā)android APP中經(jīng)常會(huì)遇到鍵盤(pán)擋住輸入框的情況,必須先把鍵盤(pán)收起,再去獲取下面輸入框焦點(diǎn),這樣用戶體驗(yàn)也非常不好,今天就給大家介紹5種完美解決android鍵盤(pán)擋住輸入框的方法2018-03-03
Flutter實(shí)現(xiàn)心動(dòng)的動(dòng)畫(huà)特效
為了追求更好的用戶體驗(yàn),有時(shí)候我們需要一個(gè)類似心跳一樣跳動(dòng)著的控件來(lái)吸引用戶的注意力。本文將利用Flutter實(shí)現(xiàn)這一動(dòng)畫(huà)特效,需要的可以參考一下2022-04-04
android獲取當(dāng)前接入點(diǎn)信息判斷是ctwap還是ctnet實(shí)例代碼
這篇文章主要介紹了android獲取當(dāng)前接入點(diǎn)信息判斷是ctwap還是ctnet的方法,大家參考使用吧2013-11-11

