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>
相關文章
- 使用C#寫出一個簡單的記事本程式C#
- 開發一款記事本
- Android開發簡單教程.docAndroid
- Python實現簡易記事本Python
- Android開發筆記[16]-簡單使用wasmedge執行時Android筆記ASM
- Android開發之ViewPager簡單使用AndroidViewpager
- android移動開發簡單的開發例項Android移動開發
- 例項 - Vue 單頁應用:記事本Vue
- 記事本介面
- QT記事本QT
- win10記事本怎麼開啟_win10的記事本在哪裡Win10
- win10桌面記事本開啟方式_win10系統怎麼開啟記事本Win10
- windows10的記事本在哪裡_win10開啟記事本的步驟WindowsWin10
- android記事本原始碼下載,很簡單的程式碼例項,絕對給力Android原始碼
- Boost Note for Mac程式設計師程式碼記事本MacC程式程式設計師
- [Android開發] 注意事項Android
- android開發中Settings結構簡單分析Android
- Android 用WebView開發簡單的瀏覽器AndroidWebView瀏覽器
- Android開發技巧——PagerAdapter再簡單的包AndroidAPT
- Android開發之FastJson概述與簡單使用AndroidASTJSON
- Android開發之道(10)Handler本質簡析與使用例項Android
- 微信小程式之簡單開發操作(一)微信小程式
- 個人專案-記事本
- 使用Python開發windows桌面程式【超簡單】PythonWindows
- Python 開發簡單爬蟲 (學習筆記)Python爬蟲筆記
- 《Python開發簡單爬蟲》實踐筆記Python爬蟲筆記
- Electron-vue開發實戰1——Main程式和Renderer程式的簡單開發VueAI
- 給win10系統右鍵選單新增“用記事本開啟”的方法Win10
- prometheus 簡單使用及簡單 middleware 開發Prometheus
- win10開機彈出desktop記事本怎麼辦_win10開機彈出記事本的解決方法Win10
- MFC開發(一)簡單同步時間應用程式
- Android開發隨記Android
- Android開發筆記Android筆記
- IPCInvoker,Android跨程式呼叫如此簡單Android
- iOS陸哥開發筆記(七) (AVFoundation簡單介紹)iOS筆記
- Java開發桌面程式學習(九)——JavaFxTemplate JavaFx模版 更簡單進行JavaFx程式開發Java
- Android開發 - 掌握ConstraintLayout(十一)複雜動畫!如此簡單!AndroidAI動畫
- 使用Flask開發簡單介面Flask