android實現(xiàn)記事本app
自己寫的一個簡單的記事本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樣式。
二、其次就是設(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>
三、編寫第二個界面樣式,第二個界面是最上面一個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>
四、將日志的數(shù)據(jù)保存在數(shù)據(jù)庫中,使用sqlite來創(chuàng)建數(shù)據(jù)庫,數(shù)據(jù)庫中有三個屬性,"_id"、"content"、"date"這三個屬性,創(chuàng)建一個NoteDB來創(chuàng)建數(shù)據(jù)庫。
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
}
}
五、實現(xiàn)點擊添加事件的跳轉(zhuǎn),在第一個頁面中點擊添加備忘錄后會跳轉(zhuǎn)到第二個界面,設(shè)置點擊事件,由一個activity跳轉(zhuǎn)到另外一個activity,我使用的是intent方式。另外,在ListView中點擊每個已記錄下來的日志也會跳轉(zhuǎn)到第二個界面,只是顯示的不是空白的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ù)庫中表的內(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
}
// 滑動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ù)滑動");
case SCROLL_STATE_IDLE:
Log.i("main", "視圖已經(jīng)停止滑動");
case SCROLL_STATE_TOUCH_SCROLL:
Log.i("main", "手指沒有離開屏幕,試圖正在滑動");
}
}
// 點擊listview中某一項的監(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
// 接受上一個頁面返回的數(shù)據(jù),并刷新頁面
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("確認(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;
}
}
六、編寫第二個跳轉(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è)置軟鍵盤自動彈出
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)按鈕的點擊事件
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);
// 獲取寫日志時間
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("執(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");
// }
}
}
七、其中,對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("確認(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,否則會和OnItemClickListener產(chǎn)生沖突。
附上長點擊刪除的效果。

在結(jié)尾附上自己的代碼,自己辛苦寫的,收取一個資源不多吧,感興趣的可以下載看看。
下載鏈接
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義控件ImageView實現(xiàn)點擊之后出現(xiàn)陰影效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件ImageView實現(xiàn)點擊之后有陰影效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
2021最新Android筆試題總結(jié)美團(tuán)Android崗職能要求
這篇文章主要介紹了2021最新Android筆試題總結(jié)以及美團(tuán)Android崗職能要求,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
Android Activity 完全結(jié)束并退出程序的實例
2013-11-11
Android EditText限制輸入字?jǐn)?shù)的方法
這篇文章主要介紹了Android EditText限制輸入字?jǐn)?shù)的方法,涉及Android針對EditText文本與字符串操作相關(guān)技巧,需要的朋友可以參考下2016-01-01
Android采取BroadcastReceiver方式自動獲取驗證碼
這篇文章主要介紹了Android采取BroadcastReceiver方式自動獲取驗證碼,感興趣的小伙伴們可以參考一下2016-08-08
Android activity動畫不生效原因及解決方案總結(jié)
android activity動畫是一個比較簡單的功能。但是使用時總會由于各種小問題導(dǎo)致動畫失效,筆者根據(jù)自己經(jīng)驗,整理了各種可能導(dǎo)致的原因,期望能對你有所幫助2021-11-11

