android实现记事本app

android实现记事本app,第1张

概述自己写的一个简单的记事本app,效果如下:一、首先是第一个界面的编写,最上面是一个TextView,中间是一个Linearlayout中嵌套一个listview布局,最下面是一个button。下面附上第一个页面的简单布局xml文件。

自己写的一个简单的记事本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",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","content!=\"\"",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 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,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","content=" + "'" + content1 + "'",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,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,"));   Cursor c = dbread.query("note","content=" + "'"    + content1 + "'",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("取消",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);  // 设置软键盘自动d出  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,data);  // if (requestCode == 3 && resultCode == 4) {  // last_content=data.getStringExtra("data");  // Log.d("LAST_STRAING",last_content+"gvg");  // }  } } 

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

public boolean onItemLongClick(AdapterVIEw<?> arg0,int which) {   }  });  builder.create();  builder.show();  return true;  } 

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

附上长点击删除的效果。

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

下载链接

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

总结

以上是内存溢出为你收集整理的android实现记事本app全部内容,希望文章能够帮你解决android实现记事本app所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1143534.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存