您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡(jiǎn)易記事本的具體代碼,供大家參考,具體內(nèi)容如下
此次做的Android簡(jiǎn)易記事本的存儲(chǔ)方式使用了SQLite數(shù)據(jù)庫(kù),然后界面的實(shí)現(xiàn)比較簡(jiǎn)單,但是,具有增刪改查的基本功能,這里可以看一下效果圖,如下:
具體操作就是長(zhǎng)按可以刪除操作,點(diǎn)擊可以進(jìn)行修改,點(diǎn)擊添加筆記按鈕可以添加一個(gè)筆記。
首先我們需要三個(gè)界面樣式一個(gè)是我們的進(jìn)入程序時(shí)的第一個(gè)界面,然后第一個(gè)界面里面有一個(gè)ListView,這個(gè)ListView需要一個(gè)xml來(lái)描述里面的各個(gè)元素,這也是第二個(gè)。還有一個(gè)就是我們的編輯頁(yè)面的界面。
三個(gè)xml描述文件如下:
activity_main.xml:進(jìn)入程序的第一個(gè)界面
<LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="記事本列表" android:textSize="20sp" android:paddingTop="10dp" android:paddingBottom="5dp" android:gravity="center"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > <ListView android:id="@+id/listNote" android:layout_margin="5dp" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout> <Button android:id="@+id/addNote" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="10dp" android:text="添加筆記" android:textSize="20sp" /> </LinearLayout>
note_item.xml:描述記事本列表中每個(gè)元素的各個(gè)控件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/noteTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:singleLine="true" android:text="" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/noteCreateTime" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="" /> </LinearLayout>
note_editor.xml:編輯界面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/noteId" android:visibility="gone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/> <EditText android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="輸入標(biāo)題"> <requestFocus /> </EditText> <EditText android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:hint="輸入內(nèi)容" android:gravity="left"> </EditText> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center" android:gravity="center"> <Button android:id="@+id/save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="10dp" android:text="保存" android:textSize="20sp" /> <Button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="10dp" android:text="取消" android:textSize="20sp" /> </LinearLayout> </LinearLayout>
現(xiàn)在我們可以考慮我們底層的數(shù)據(jù)庫(kù)的操作了,這里有一個(gè)類專門用于與數(shù)據(jù)庫(kù)打交道,如下:
DBService.java
public class DBService { private static SQLiteDatabase db = null; static { //新建或者打開(kāi)db db = SQLiteDatabase.openOrCreateDatabase("data/data/cn.lger.notebook/NoteBook.db", null); String sql = "create table NoteBook(_id integer primary key autoincrement,title varchar(255),content TEXT, createTime varchar(25))"; //判斷是否存在表NoteBook,如果不存在會(huì)拋出異常,捕捉異常后創(chuàng)建表 try{ db.rawQuery("select count(1) from NoteBook ",null); }catch(Exception e){ db.execSQL(sql); } } public static SQLiteDatabase getSQLiteDatabase(){ return db; } public static Cursor queryAll(){ return db.rawQuery("select * from NoteBook ",null); } public static Cursor queryNoteById(Integer id){ return db.rawQuery("select * from NoteBook where _id =?",new String[]{id.toString()}); } public static void deleteNoteById(Integer id){ if(id == null) return ; db.delete("NoteBook", "_id=?", new String[]{id.toString()}); } public static void updateNoteById(Integer id, ContentValues values){ db.update("NoteBook", values, "_id=?", new String[]{id.toString()}); } /** * 添加一個(gè)筆記,并且記錄當(dāng)前添加的時(shí)間 * @param values 表中的各個(gè)字段值 */ public static void addNote(ContentValues values){ values.put("createTime", DateFormat.format("yyyy-MM-dd kk:mm:ss", System.currentTimeMillis()).toString()); db.insert("NoteBook", null, values); } }
下面我們?cè)谶M(jìn)入第一個(gè)界面的時(shí)候需要訪問(wèn)數(shù)據(jù)庫(kù)并且將數(shù)據(jù)的值不斷的更新(比如進(jìn)行了刪除操作的時(shí)候或者添加操作之后需要刷新),這樣,我們就可能需要重寫Activity的onResume(),這樣就可以調(diào)用Cursor的requery()來(lái)刷新我們列表中ListView的結(jié)果。還有我們需要長(zhǎng)按刪除,點(diǎn)擊修改,添加筆記這些都需要監(jiān)聽(tīng)事件,因此,這里還要設(shè)置監(jiān)聽(tīng)
具體MainActivity.java的代碼如下:
public class MainActivity extends Activity { private Cursor listItemCursor = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 設(shè)置添加筆記按鈕事件,切換activity this.findViewById(R.id.addNote).setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { Intent in = new Intent(); in.setClassName(getApplicationContext(), "cn.lger.notebook.NoteEditActivity"); startActivity(in); } }); // 查詢所有筆記,并將筆記展示出來(lái) listItemCursor = DBService.queryAll(); SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.note_item, listItemCursor, new String[] { "_id", "title", "createTime" }, new int[] { R.id.noteId, R.id.noteTitle, R.id.noteCreateTime }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); ((ListView) this.findViewById(R.id.listNote)).setAdapter(adapter); initListNoteListener(); } /** * 初始化筆記列表的長(zhǎng)按和點(diǎn)擊事件 */ private void initListNoteListener() { // 長(zhǎng)按刪除 ((ListView) this.findViewById(R.id.listNote)) .setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, final long id) { new AlertDialog.Builder(MainActivity.this) .setTitle("提示框") .setMessage("確認(rèn)刪除該筆記??") .setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0,int arg1) { DBService.deleteNoteById((int) id); //刪除后刷新列表 MainActivity.this.onResume(); Toast.makeText( MainActivity.this, "刪除成功!!", Toast.LENGTH_LONG) .show(); } }).setNegativeButton("取消", null).show(); return false; } }); //點(diǎn)擊進(jìn)行修改操作 ((ListView) this.findViewById(R.id.listNote)) .setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent in = new Intent(); in.setClassName(view.getContext(), "cn.lger.notebook.NoteEditActivity"); // 將id數(shù)據(jù)放置到Intent,切換視圖后可以將數(shù)據(jù)傳遞過(guò)去 in.putExtra("id", id); startActivity(in); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } /** * 當(dāng)從另一個(gè)視圖進(jìn)入該視圖會(huì)調(diào)用該方法 */ @Override protected void onResume() { super.onResume(); // 要求刷新主頁(yè)列表筆記 if (listItemCursor != null) { listItemCursor.requery(); } } }
上面的代碼中還涉及到了一個(gè)視圖切換后的傳遞信息的操作,就是通過(guò)Intent的putExtra(key, value)這樣可以在切換后的視圖中調(diào)用函數(shù)getIntent().get~Extra(key, replace);來(lái)接收傳遞的數(shù)據(jù)。
下面是我們的編輯界面中對(duì)應(yīng)的具體實(shí)現(xiàn)代碼,這里有判斷是使用更新操作還是添加操作,主要是判斷MainActivity.java有沒(méi)有傳遞過(guò)來(lái)id,如果有就是通過(guò)這個(gè)id來(lái)更新操作,沒(méi)有就是添加操作。
編輯界面對(duì)應(yīng)的具體實(shí)現(xiàn)代碼如下:
NoteEditActivity.java
public class NoteEditActivity extends Activity { private EditText titleEditText = null; private EditText contentEditText = null; private String noteId = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.note_editor); titleEditText = (EditText) NoteEditActivity.this .findViewById(R.id.title); contentEditText = (EditText) NoteEditActivity.this .findViewById(R.id.content); initNoteEditValue(); //取消按鈕監(jiān)聽(tīng) this.findViewById(R.id.cancel).setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { NoteEditActivity.this.finish(); } }); this.findViewById(R.id.save).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { final String title = titleEditText.getText().toString(); final String content = contentEditText.getText().toString(); //判斷標(biāo)題和內(nèi)容是否為空,不為空才能保存 if ("".equals(title) || "".equals(content)) { Toast.makeText(NoteEditActivity.this, "標(biāo)題或者內(nèi)容不能為空", Toast.LENGTH_LONG).show(); return; } //提示保存 new AlertDialog.Builder(NoteEditActivity.this) .setTitle("提示框") .setMessage("確定保存筆記嗎??") .setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { ContentValues values = new ContentValues(); values.put("title", title); values.put("content", content); //如果noteId不為空那么就是更新操作,為空就是添加操作 if (null == noteId || "".equals(noteId)) DBService.addNote(values); else DBService.updateNoteById( Integer.valueOf(noteId), values); //結(jié)束當(dāng)前activity NoteEditActivity.this.finish(); Toast.makeText(NoteEditActivity.this, "保存成功??!", Toast.LENGTH_LONG).show(); } }).setNegativeButton("取消", null).show(); } }); } /** * 初始化編輯頁(yè)面的值(如果進(jìn)入該頁(yè)面時(shí)存在一個(gè)id的話),比如標(biāo)題,內(nèi)容。 */ private void initNoteEditValue() { // 從Intent中獲取id的值 long id = this.getIntent().getLongExtra("id", -1L); // 如果有傳入id那么id!=-1 if (id != -1L) { // 使用noteId保存id noteId = String.valueOf(id); // 查詢?cè)搃d的筆記 Cursor cursor = DBService.queryNoteById((int) id); if (cursor.moveToFirst()) { // 將內(nèi)容提取出來(lái) titleEditText.setText(cursor.getString(1)); contentEditText.setText(cursor.getString(2)); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
以上就將我們的安卓簡(jiǎn)易記事本完成了,源碼已經(jīng)上傳GitHub。
界面采用了拿來(lái)主義,可以參考下面文章
android實(shí)現(xiàn)記事本app
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。