自己写的一个简单的记事本app,效果如下:
一、首先是第一个界面的编写,最上面是一个TextView,中间是一个Linearlayout中嵌套一个listview布局,最下面是一个button。下面附上第一个页面的简单布局xml文件。
至于button的样式btn_selector就是自己定义的button样式。
二、其次就是设置ListView中数据显示的xml文件,代码如下:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
三、编写第二个界面样式,第二个界面是最上面一个linearlayout,里面包含两个button和一个TextView。代码如下:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
四、将日志的数据保存在数据库中,使用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
六、编写第二个跳转后界面的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<&#63;> 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产生冲突。
附上长点击删除的效果。
在结尾附上自己的代码,自己辛苦写的,收取一个资源不多吧,感兴趣的可以下载看看。
下载链接
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。