Android 開發簡單記事本程式
簡單介紹一下功能:簡單記事本只能新增文字內容,首頁用ListView顯示所有儲存的事項和儲存的時間,新增頁面新增內容,點選首頁的ListView可以檢視內容,刪除內容。
先看一下執行效果:
先建立資料庫:
package com.engineer.shizhibin.note;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class NoteDb extends SQLiteOpenHelper {
public static final String TABLE_NAME = "notes";
public static final String CONTENT = "content";
public static final String ID = "_id";
public static final String TIME = "time";
public NoteDb(Context context) {
super(context, "notes", null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql ="create table "+TABLE_NAME+" ( "+ID+" integer primary key AUTOINCREMENT, "+CONTENT
+" TEXT NOT NULL, "+TIME+" TEXT NOT NULL )";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
然後是MainActivity:
有顯示目錄的ListView,和新增按鈕。他們都要設定點選監聽事件。點選新增 跳轉到新增文字的activity 點選ListView跳轉到Show Activity。
onResume() 在 Activity 從 Pause 狀態轉換到 Active 狀態時被呼叫。在這個方法中訪問SQLite資料庫,通過Adapter將條目的內容和新增時間顯示在ListView中。看程式碼:
package com.engineer.shizhibin.note;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private Button mButton;
private ListView mList;
private Intent mIntent;
private MyAdapter mAdapter;
private NoteDb mNotedb;
private Cursor cursor;
private SQLiteDatabase dbreader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mList = (ListView) this.findViewById(R.id.list);
mNotedb = new NoteDb(this);
dbreader = mNotedb.getReadableDatabase();
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
cursor.moveToPosition(i);
Intent intent = new Intent(MainActivity.this,ShowContent.class);
intent.putExtra(NoteDb.ID,cursor.getInt(cursor.getColumnIndex(NoteDb.ID)));
intent.putExtra(NoteDb.CONTENT,cursor.getString(cursor.getColumnIndex(NoteDb.CONTENT)));
intent.putExtra(NoteDb.TIME,cursor.getString(cursor.getColumnIndex(NoteDb.TIME)));
startActivity(intent);
}
});
}
public void add(View v) {
mIntent = new Intent(MainActivity.this,AddContent.class);
startActivity(mIntent);
}
public void selectDb() {
cursor = dbreader.query
(NoteDb.TABLE_NAME,null,null,null,null,null,null);
mAdapter = new MyAdapter(this,cursor);
mList.setAdapter(mAdapter);
}
@Override
protected void onResume() {
super.onResume();
selectDb();
}
}
為了方便程式擴充套件,以後有可能新增圖片、視訊內容 我用了自定義的Adapter 繼承自BadeAdapter:
package com.engineer.shizhibin.note;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private Context mContext;
private Cursor mCursor;
private LinearLayout mLayout;
public MyAdapter(Context mContext,Cursor mCursor) {
this.mContext = mContext;
this.mCursor = mCursor;
}
@Override
public int getCount() {
return mCursor.getCount();
}
@Override
public Object getItem(int position) {
return mCursor.getPosition();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
LayoutInflater inflater = LayoutInflater.from(mContext);
mLayout = (LinearLayout) inflater.inflate(R.layout.item,null);
TextView content = (TextView) mLayout.findViewById(R.id.list_content);
TextView time = (TextView) mLayout.findViewById(R.id.list_time);
mCursor.moveToPosition(position);
String dbcontext = mCursor.getString(mCursor.getColumnIndex("content"));
String dbtime = mCursor.getString(mCursor.getColumnIndex("time"));
content.setText(dbcontext);
time.setText(dbtime);
return mLayout;
}
}
新增條目,向資料中新增資料用ContentValues以鍵值對的方式 非常方便:
package com.engineer.shizhibin.note;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AddContent extends AppCompatActivity {
private EditText mEt;
private NoteDb mDb;
private SQLiteDatabase mSqldb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_content);
mEt = (EditText) this.findViewById(R.id.text);
mDb = new NoteDb(this);
mSqldb = mDb.getWritableDatabase();
}
public void save(View v) {
DbAdd();
finish();
}
public void cancle(View v) {
mEt.setText("");
finish();
}
public void DbAdd() {
ContentValues cv = new ContentValues();
cv.put(NoteDb.CONTENT,mEt.getText().toString());
cv.put(NoteDb.TIME,getTime());
mSqldb.insert(NoteDb.TABLE_NAME,null,cv);
}
public String getTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date = new Date();
String str = sdf.format(date);
return str;
}
}
檢視條目:
package com.engineer.shizhibin.note;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class ShowContent extends AppCompatActivity {
private TextView mTextview;
private TextView time;
private NoteDb mDb;
private SQLiteDatabase mSql;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_content);
mTextview = (TextView)this.findViewById(R.id.showtext);
time = (TextView)this.findViewById(R.id.showtime);
mDb = new NoteDb(this);
mSql = mDb.getWritableDatabase();
mTextview.setText(getIntent().getStringExtra(NoteDb.CONTENT));
time.setText(getIntent().getStringExtra(NoteDb.TIME));
}
public void delete(View v) {
int id = getIntent().getIntExtra(NoteDb.ID,0);
mSql.delete(NoteDb.TABLE_NAME," _id = " + id,null);
finish();
}
public void goBack(View v) {
finish();
}
}
MainActivity佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/back"
tools:context="com.engineer.shizhibin.note.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新增"
android:onClick="add"/>
</LinearLayout>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:layout_marginTop="20dp">
</ListView>
</LinearLayout>
新增Activity佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/back"
tools:context="com.engineer.shizhibin.note.AddContent">
<EditText
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="top"
android:textColor="#000"
android:hint="有了記事本,我再也不會忘記事情了!"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="儲存"
android:onClick="save"/>
<Button
android:id="@+id/cancle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消"
android:onClick="cancle"/>
</LinearLayout>
</LinearLayout>
檢視頁佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@drawable/back"
android:layout_height="match_parent"
tools:context="com.engineer.shizhibin.note.ShowContent">
<TextView
android:id="@+id/showtime"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:textSize="20sp"/>
<TextView
android:id="@+id/showtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="top"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textColor="#000"
android:hint="有了記事本,我再也不會忘記事情了!"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="刪除"
android:onClick="delete"/>
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="返回"
android:onClick="goBack"/>
</LinearLayout>
</LinearLayout>
ListView佈局
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_content"
android:textColor="#000"
android:textSize="20sp"
android:text="tv"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_time"
android:textColor="#000"
android:textSize="20sp"
android:text="time"/>
</LinearLayout>
</LinearLayout>
相關文章
- 開發一款記事本
- Android開發簡單教程.docAndroid
- Android開發筆記[16]-簡單使用wasmedge執行時Android筆記ASM
- 記事本
- [Android開發] 注意事項Android
- 記事本介面
- Shell 記事本
- win10記事本怎麼開啟_win10的記事本在哪裡Win10
- windows10的記事本在哪裡_win10開啟記事本的步驟WindowsWin10
- Boost Note for Mac程式設計師程式碼記事本MacC程式程式設計師
- win10桌面記事本開啟方式_win10系統怎麼開啟記事本Win10
- Android開發 - 掌握ConstraintLayout(十一)複雜動畫!如此簡單!AndroidAI動畫
- Android開發隨記Android
- Android開發筆記Android筆記
- Python 開發簡單爬蟲 (學習筆記)Python爬蟲筆記
- 《Python開發簡單爬蟲》實踐筆記Python爬蟲筆記
- 微信小程式之簡單開發操作(一)微信小程式
- prometheus 簡單使用及簡單 middleware 開發Prometheus
- Android事件分發機制簡單理解Android事件
- Android開發——說說Adapter那點事AndroidAPT
- win10開機彈出desktop記事本怎麼辦_win10開機彈出記事本的解決方法Win10
- 基於元件化開發,一個簡單的Android專案框架元件化Android框架
- 直播平臺開發,Android端簡單的顏色背景變換Android
- 多功能記事本:Notebooks for MacMac
- Notebooks for Mac多功能記事本Mac
- MFC開發(一)簡單同步時間應用程式
- 逮蝦戶!Android程式除錯竟簡單如斯Android除錯
- Android 酒店客房管理簡單小程式Android
- Java開發桌面程式學習(九)——JavaFxTemplate JavaFx模版 更簡單進行JavaFx程式開發Java
- JUC併發程式設計學習筆記(六)Callable(簡單)程式設計筆記
- Android 開發學習筆記Android筆記
- 直播軟體開發,Android自定義簡單的音訊波譜viewAndroid音訊View
- 你知道的Electron小小記事本
- C# 記事本儲存logC#
- 簡單開發的DOS指令
- 使用Flask開發簡單介面Flask
- 開發筆記1|Java程式碼規約清單筆記Java
- Java開發筆記(四十八)類的簡單繼承Java筆記繼承