欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android实现记事本app

程序员文章站 2022-03-22 23:50:20
自己写的一个简单的记事本app,效果如下: 一、首先是第一个界面的编写,最上面是一个textview,中间是一个linearlayout中嵌套一个listview布局...

自己写的一个简单的记事本app,效果如下:

android实现记事本app

一、首先是第一个界面的编写,最上面是一个textview,中间是一个linearlayout中嵌套一个listview布局,最下面是一个button。下面附上第一个页面的简单布局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样式。

二、其次就是设置listview中数据显示的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> 

三、编写第二个界面样式,第二个界面是最上面一个linearlayout,里面包含两个button和一个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> 

四、将日志的数据保存在数据库中,使用sqlite来创建数据库,数据库中有三个属性,"_id"、"content"、"date"这三个属性,创建一个notedb来创建数据库。

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,"+"'写作业',"+"'晚上要写作业的干活'"+")"; 
// log.d("sql1", sql1); 
// db.execsql(sql1); 
// contentvalues values=new contentvalues(); 
// values.put("id",1); 
// values.put("content","写作业"); 
// 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 
 
 } 
 
} 

五、实现点击添加事件的跳转,在第一个页面中点击添加备忘录后会跳转到第二个界面,设置点击事件,由一个activity跳转到另外一个activity,我使用的是intent方式。另外,在listview中点击每个已记录下来的日志也会跳转到第二个界面,只是显示的不是空白的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(); 
 // 清空数据库中表的内容 
 //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 
 
 } 
 
 // 滑动listview监听事件 
 @override 
 public void onscrollstatechanged(abslistview arg0, int arg1) { 
 // todo auto-generated method stub 
 switch (arg1) { 
 case scroll_state_fling: 
  log.i("main", "用户在手指离开屏幕之前,由于用力的滑了一下,视图能依靠惯性继续滑动"); 
 case scroll_state_idle: 
  log.i("main", "视图已经停止滑动"); 
 case scroll_state_touch_scroll: 
  log.i("main", "手指没有离开屏幕,试图正在滑动"); 
 } 
 } 
 
 // 点击listview中某一项的监听事件 
 @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 
 // 接受上一个页面返回的数据,并刷新页面 
 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(); 
 } 
 } 
 
 // 点击listview中某一项长时间的点击事件 
 @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("确认删除吗?"); 
 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; 
 } 
 
} 

六、编写第二个跳转后界面的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); 
 // 设置无标题 
 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); 
 // 设置软键盘自动弹出 
 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); 
 // 确认按钮的点击事件 
 btn_ok = (button) findviewbyid(r.id.btn_ok); 
 btn_ok.setonclicklistener(new onclicklistener() { 
  public void onclick(view arg0) { 
  // 获取日志内容 
  string content = et_content.gettext().tostring(); 
  log.d("log1", content); 
  // 获取写日志时间 
  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 + ""); 
  // 添加一个新的日志 
  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); 
   } 
  } 
  // 查看并修改一个已有的日志 
  else { 
   log.d("执行命令", "执行了该函数"); 
   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"); 
 // } 
 } 
} 

七、其中,对listview添加onitemlongclicklistener,长点击之后会弹出一个dialog对话框提醒要不要删除该日志文件。

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("确认删除吗?"); 
 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; 
 } 

注意最后将返回值设为true,否则会和onitemclicklistener产生冲突。

附上长点击删除的效果。

android实现记事本app

在结尾附上自己的代码,自己辛苦写的,收取一个资源不多吧,感兴趣的可以下载看看。

下载链接

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。