Android 開發簡單記事本程式

bingoCoder發表於2018-06-23

    簡單介紹一下功能:簡單記事本只能新增文字內容,首頁用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>


    

相關文章