這篇文章將為大家詳細(xì)講解有關(guān)android如何實(shí)現(xiàn)記事本app,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)主要從事網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)霸州,10年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
自己寫的一個(gè)簡(jiǎn)單的記事本app,效果如下:
一、首先是第一個(gè)界面的編寫,最上面是一個(gè)TextView,中間是一個(gè)Linearlayout中嵌套一個(gè)listview布局,最下面是一個(gè)button。下面附上第一個(gè)頁(yè)面的簡(jiǎn)單布局xml文件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/darker_gray" android:orientation="vertical" > <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="記事本列表" android:textSize="20sp" android:paddingTop="10dp" android:paddingBottom="5dp" android:background="#039DCF" android:gravity="center"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > <ListView android:id="@+id/listview" android:layout_margin="5dp" android:background="#81966F" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout> <Button android:id="@+id/btn_editnote" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/btn_selctor" android:layout_gravity="center" android:layout_marginBottom="10dp" android:text="添加備忘錄" android:textSize="20sp" /> </LinearLayout>
至于button的樣式btn_selector就是自己定義的button樣式。
二、其次就是設(shè)置ListView中數(shù)據(jù)顯示的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/tv_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:singleLine="true" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/tv_date" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="TextView" /> </LinearLayout>
三、編寫第二個(gè)界面樣式,第二個(gè)界面是最上面一個(gè)linearlayout,里面包含兩個(gè)button和一個(gè)TextView。代碼如下:
<?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:background="#FFAE99" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/btn_selctor" android:text="取消" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="編輯備忘錄" android:textSize="20sp" /> <TextView android:id="@+id/tv_date" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="" /> </LinearLayout> <Button android:id="@+id/btn_ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/btn_selctor" android:text="保存" /> </LinearLayout> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginLeft="2dp" android:orientation="vertical" > <EditText android:id="@+id/et_content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#FFAE99" android:textSize="20sp" /> </LinearLayout> </ScrollView> </LinearLayout>
四、將日志的數(shù)據(jù)保存在數(shù)據(jù)庫(kù)中,使用sqlite來創(chuàng)建數(shù)據(jù)庫(kù),數(shù)據(jù)庫(kù)中有三個(gè)屬性,"_id"、"content"、"date"這三個(gè)屬性,創(chuàng)建一個(gè)NoteDB來創(chuàng)建數(shù)據(jù)庫(kù)。
package com.example.datenote; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class NotesDB extends SQLiteOpenHelper { public static final String TABLE_NAME_NOTES = "note"; public static final String COLUMN_NAME_ID = "_id"; public static final String COLUMN_NAME_NOTE_CONTENT = "content"; public static final String COLUMN_NAME_NOTE_DATE = "date"; public NotesDB(Context context) { super(context, "note", null, 1); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { String sql = "CREATE TABLE " + TABLE_NAME_NOTES + "(" + COLUMN_NAME_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NAME_NOTE_CONTENT + " TEXT NOT NULL DEFAULT\"\"," + COLUMN_NAME_NOTE_DATE + " TEXT NOT NULL DEFAULT\"\"" + ")"; Log.d("SQL", sql); db.execSQL(sql); // String sql1="insert into "+TABLE_NAME_NOTES+"values("+"1,"+"'寫作業(yè)',"+"'晚上要寫作業(yè)的干活'"+")"; // Log.d("SQL1", sql1); // db.execSQL(sql1); // ContentValues values=new ContentValues(); // values.put("id",1); // values.put("content","寫作業(yè)"); // values.put("date", "2013-1-2"); // db.insert("note", null, values); } @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { // TODO Auto-generated method stub } }
五、實(shí)現(xiàn)點(diǎn)擊添加事件的跳轉(zhuǎn),在第一個(gè)頁(yè)面中點(diǎn)擊添加備忘錄后會(huì)跳轉(zhuǎn)到第二個(gè)界面,設(shè)置點(diǎn)擊事件,由一個(gè)activity跳轉(zhuǎn)到另外一個(gè)activity,我使用的是intent方式。另外,在ListView中點(diǎn)擊每個(gè)已記錄下來的日志也會(huì)跳轉(zhuǎn)到第二個(gè)界面,只是顯示的不是空白的EditText,而是包含日志的EditText。MainActivity如下:
package com.example.datenote; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements OnScrollListener, OnItemClickListener, OnItemLongClickListener { private Context mContext; private ListView listview; private SimpleAdapter simp_adapter; private List<Map<String, Object>> dataList; private Button addNote; private TextView tv_content; private NotesDB DB; private SQLiteDatabase dbread; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); tv_content = (TextView) findViewById(R.id.tv_content); listview = (ListView) findViewById(R.id.listview); dataList = new ArrayList<Map<String, Object>>(); addNote = (Button) findViewById(R.id.btn_editnote); mContext = this; addNote.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { noteEdit.ENTER_STATE = 0; Intent intent = new Intent(mContext, noteEdit.class); Bundle bundle = new Bundle(); bundle.putString("info", ""); intent.putExtras(bundle); startActivityForResult(intent, 1); } }); DB = new NotesDB(this); dbread = DB.getReadableDatabase(); // 清空數(shù)據(jù)庫(kù)中表的內(nèi)容 //dbread.execSQL("delete from note"); RefreshNotesList(); listview.setOnItemClickListener(this); listview.setOnItemLongClickListener(this); listview.setOnScrollListener(this); } public void RefreshNotesList() { int size = dataList.size(); if (size > 0) { dataList.removeAll(dataList); simp_adapter.notifyDataSetChanged(); listview.setAdapter(simp_adapter); } simp_adapter = new SimpleAdapter(this, getData(), R.layout.item, new String[] { "tv_content", "tv_date" }, new int[] { R.id.tv_content, R.id.tv_date }); listview.setAdapter(simp_adapter); } private List<Map<String, Object>> getData() { Cursor cursor = dbread.query("note", null, "content!=\"\"", null, null, null, null); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex("content")); String date = cursor.getString(cursor.getColumnIndex("date")); Map<String, Object> map = new HashMap<String, Object>(); map.put("tv_content", name); map.put("tv_date", date); dataList.add(map); } cursor.close(); return dataList; } @Override public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } // 滑動(dòng)listview監(jiān)聽事件 @Override public void onScrollStateChanged(AbsListView arg0, int arg1) { // TODO Auto-generated method stub switch (arg1) { case SCROLL_STATE_FLING: Log.i("main", "用戶在手指離開屏幕之前,由于用力的滑了一下,視圖能依靠慣性繼續(xù)滑動(dòng)"); case SCROLL_STATE_IDLE: Log.i("main", "視圖已經(jīng)停止滑動(dòng)"); case SCROLL_STATE_TOUCH_SCROLL: Log.i("main", "手指沒有離開屏幕,試圖正在滑動(dòng)"); } } // 點(diǎn)擊listview中某一項(xiàng)的監(jiān)聽事件 @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { noteEdit.ENTER_STATE = 1; // Log.d("arg2", arg2 + ""); // TextView // content=(TextView)listview.getChildAt(arg2).findViewById(R.id.tv_content); // String content1=content.toString(); String content = listview.getItemAtPosition(arg2) + ""; String content1 = content.substring(content.indexOf("=") + 1, content.indexOf(",")); Log.d("CONTENT", content1); Cursor c = dbread.query("note", null, "content=" + "'" + content1 + "'", null, null, null, null); while (c.moveToNext()) { String No = c.getString(c.getColumnIndex("_id")); Log.d("TEXT", No); // Intent intent = new Intent(mContext, noteEdit.class); // intent.putExtra("data", text); // setResult(4, intent); // // intent.putExtra("data",text); // startActivityForResult(intent, 3); Intent myIntent = new Intent(); Bundle bundle = new Bundle(); bundle.putString("info", content1); noteEdit.id = Integer.parseInt(No); myIntent.putExtras(bundle); myIntent.setClass(MainActivity.this, noteEdit.class); startActivityForResult(myIntent, 1); } } @Override // 接受上一個(gè)頁(yè)面返回的數(shù)據(jù),并刷新頁(yè)面 protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == 2) { RefreshNotesList(); } } // 點(diǎn)擊listview中某一項(xiàng)長(zhǎng)時(shí)間的點(diǎn)擊事件 @Override public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { final int n=arg2; Builder builder = new AlertDialog.Builder(this); builder.setTitle("刪除該日志"); builder.setMessage("確認(rèn)刪除嗎?"); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String content = listview.getItemAtPosition(n) + ""; String content1 = content.substring(content.indexOf("=") + 1, content.indexOf(",")); Cursor c = dbread.query("note", null, "content=" + "'" + content1 + "'", null, null, null, null); while (c.moveToNext()) { String id = c.getString(c.getColumnIndex("_id")); String sql_del = "update note set content='' where _id=" + id; dbread.execSQL(sql_del); RefreshNotesList(); } } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.create(); builder.show(); return true; } }
六、編寫第二個(gè)跳轉(zhuǎn)后界面的Activity,如下:
package com.example.datenote; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteStatement; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class noteEdit extends Activity { private TextView tv_date; private EditText et_content; private Button btn_ok; private Button btn_cancel; private NotesDB DB; private SQLiteDatabase dbread; public static int ENTER_STATE = 0; public static String last_content; public static int id; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // 設(shè)置無標(biāo)題 requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.edit); tv_date = (TextView) findViewById(R.id.tv_date); Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); String dateString = sdf.format(date); tv_date.setText(dateString); et_content = (EditText) findViewById(R.id.et_content); // 設(shè)置軟鍵盤自動(dòng)彈出 getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); DB = new NotesDB(this); dbread = DB.getReadableDatabase(); Bundle myBundle = this.getIntent().getExtras(); last_content = myBundle.getString("info"); Log.d("LAST_CONTENT", last_content); et_content.setText(last_content); // 確認(rèn)按鈕的點(diǎn)擊事件 btn_ok = (Button) findViewById(R.id.btn_ok); btn_ok.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // 獲取日志內(nèi)容 String content = et_content.getText().toString(); Log.d("LOG1", content); // 獲取寫日志時(shí)間 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dateNum = sdf.format(date); String sql; String sql_count = "SELECT COUNT(*) FROM note"; SQLiteStatement statement = dbread.compileStatement(sql_count); long count = statement.simpleQueryForLong(); Log.d("COUNT", count + ""); Log.d("ENTER_STATE", ENTER_STATE + ""); // 添加一個(gè)新的日志 if (ENTER_STATE == 0) { if (!content.equals("")) { sql = "insert into " + NotesDB.TABLE_NAME_NOTES + " values(" + count + "," + "'" + content + "'" + "," + "'" + dateNum + "')"; Log.d("LOG", sql); dbread.execSQL(sql); } } // 查看并修改一個(gè)已有的日志 else { Log.d("執(zhí)行命令", "執(zhí)行了該函數(shù)"); String updatesql = "update note set content='" + content + "' where _id=" + id; dbread.execSQL(updatesql); // et_content.setText(last_content); } Intent data = new Intent(); setResult(2, data); finish(); } }); btn_cancel = (Button) findViewById(R.id.btn_cancel); btn_cancel.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { finish(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); // if (requestCode == 3 && resultCode == 4) { // last_content=data.getStringExtra("data"); // Log.d("LAST_STRAING", last_content+"gvg"); // } } }
七、其中,對(duì)ListView添加OnItemLongclicklistener,長(zhǎng)點(diǎn)擊之后會(huì)彈出一個(gè)dialog對(duì)話框提醒要不要?jiǎng)h除該日志文件。
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { final int n=arg2; Builder builder = new AlertDialog.Builder(this); builder.setTitle("刪除該日志"); builder.setMessage("確認(rèn)刪除嗎?"); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String content = listview.getItemAtPosition(n) + ""; String content1 = content.substring(content.indexOf("=") + 1, content.indexOf(",")); Cursor c = dbread.query("note", null, "content=" + "'" + content1 + "'", null, null, null, null); while (c.moveToNext()) { String id = c.getString(c.getColumnIndex("_id")); String sql_del = "update note set content='' where _id=" + id; dbread.execSQL(sql_del); RefreshNotesList(); } } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.create(); builder.show(); return true; }
注意最后將返回值設(shè)為true,否則會(huì)和OnItemClickListener產(chǎn)生沖突。
附上長(zhǎng)點(diǎn)擊刪除的效果。
關(guān)于“android如何實(shí)現(xiàn)記事本app”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
當(dāng)前名稱:android如何實(shí)現(xiàn)記事本app
本文網(wǎng)址:http://jinyejixie.com/article8/gpehop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站策劃、App設(shè)計(jì)、網(wǎng)站設(shè)計(jì)、網(wǎng)站改版、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)