安卓简易笔记app

来源:互联网 发布:阿里云企业备案流程图 编辑:程序博客网 时间:2024/05/18 03:34
功能描述:
(1) 可以查看过去的笔记
(2) 点击悬浮按钮可以添加笔记
(3) 点击列表子项可以修改笔记
(4) 点击彩色按钮可以修改背景
(5) 长按列表子项可以删除笔记

布局
activity_main

<?xml version="1.0"?>-<android.support.design.widget.CoordinatorLayout android:orientation="vertical" android:background="@color/colorWhite" android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"><include layout="@layout/content_main"/><android.support.design.widget.FloatingActionButton android:layout_height="wrap_content" android:layout_width="wrap_content" app:srcCompat="@android:drawable/ic_media_play" android:layout_margin="16dp" android:layout_gravity="bottom|end" android:id="@+id/fab"/></android.support.design.widget.CoordinatorLayout>

content_main
<?xml version="1.0"?>-<LinearLayout android:orientation="vertical" android:background="@color/colorWhite" android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"><TextView android:background="@color/colorPrimary" android:layout_height="wrap_content" android:layout_width="match_parent" android:textSize="27sp" android:textColor="@color/colorWhite" android:text=" Note"/>-<LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1"><ListView android:background="@color/colorWhite" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/listview"> </ListView></LinearLayout></LinearLayout>

edit

<?xml version="1.0" encoding="UTF-8"?>-<LinearLayout android:orientation="vertical" android:background="@color/colorWhite" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">-<LinearLayout android:orientation="horizontal" android:layout_height="80dp" android:layout_width="match_parent" android:layout_weight="1"><Button android:background="@color/color0" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_weight="1" android:id="@+id/button0"> </Button><Button android:background="@color/color1" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_weight="1" android:id="@+id/button1"> </Button><Button android:background="@color/color2" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_weight="1" android:id="@+id/button2"> </Button><Button android:background="@color/color3" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_weight="1" android:id="@+id/button3"> </Button></LinearLayout>-<LinearLayout android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" android:layout_weight="1"><TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/tv_date" android:textSize="15sp"/><EditText android:background="@color/colorWhite" android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/et_content" android:textSize="20sp" android:gravity="top"> </EditText></LinearLayout></LinearLayout>

item

<?xml version="1.0" encoding="UTF-8"?>-<LinearLayout android:orientation="vertical" android:background="@drawable/back" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"><TextView android:background="#baffffff" android:layout_height="wrap_content" android:layout_width="match_parent" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#737272" android:id="@+id/tv_content"/><TextView android:background="#baffffff" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/tv_date"/></LinearLayout>

功能

MainActivity
package com.example.myapplication;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.support.design.widget.FloatingActionButton;import android.view.View;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.EditText;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.TextView;public class MainActivity extends Activity implements OnScrollListener, OnItemClickListener, OnItemLongClickListener {    private NotesDB DB;    private SQLiteDatabase dbread;    private Context mContext;    private ListView listview;    private FloatingActionButton fab;    private SimpleAdapter simp_adapter;    private List<Map<String, Object>> dataList;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mContext = this;        listview = (ListView) findViewById(R.id.listview);        dataList = new ArrayList<Map<String, Object>>();        fab = (FloatingActionButton) findViewById(R.id.fab);        fab.setOnClickListener(new View.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();        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) {    }    @Override    public void onScrollStateChanged(AbsListView arg0, int arg1) {    }    @Override    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {        NoteEdit.ENTER_STATE = 1;        String content = listview.getItemAtPosition(arg2) + "";        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 No = c.getString(c.getColumnIndex("_id"));            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    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        if (requestCode == 1 && resultCode == 2) {            RefreshNotesList();        }    }    @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("确定删除吗?");        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 = "delete from note 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;    }}

NoteEdit
package com.example.myapplication;import java.text.SimpleDateFormat;import java.util.Date;import android.app.Activity;import android.content.Intent;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteStatement;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.view.WindowManager;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.TextView;public class NoteEdit extends Activity {private NotesDB DB;private SQLiteDatabase dbread;private Button button0;private Button button1;private Button button2;private Button button3;private TextView tv_date;private EditText et_content;public static int id;public static int ENTER_STATE = 0;public static String last_content;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.edit);tv_date = (TextView) findViewById(R.id.tv_date);Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");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");et_content.setText(last_content);button0 = (Button)findViewById(R.id.button0);button1 = (Button)findViewById(R.id.button1);button2 = (Button)findViewById(R.id.button2);button3 = (Button)findViewById(R.id.button3);et_content = (EditText)findViewById(R.id.et_content);button0.setOnClickListener(new View.OnClickListener() {boolean blank0 = true;@Overridepublic void onClick(View v) {if (blank0){et_content.setBackgroundColor(getResources().getColor(R.color.color0));tv_date.setBackgroundColor(getResources().getColor(R.color.color0));blank0 = false;} else {et_content.setBackgroundColor(getResources().getColor(R.color.colorWhite));tv_date.setBackgroundColor(getResources().getColor(R.color.colorWhite));blank0 = true;}}});button1.setOnClickListener(new View.OnClickListener() {boolean blank1 = true;@Overridepublic void onClick(View v) {if (blank1){et_content.setBackgroundColor(getResources().getColor(R.color.color1));tv_date.setBackgroundColor(getResources().getColor(R.color.color1));blank1 = false;} else {et_content.setBackgroundColor(getResources().getColor(R.color.colorWhite));tv_date.setBackgroundColor(getResources().getColor(R.color.colorWhite));blank1 = true;}}});button2.setOnClickListener(new View.OnClickListener() {boolean blank2 = true;@Overridepublic void onClick(View v) {if (blank2){et_content.setBackgroundColor(getResources().getColor(R.color.color2));tv_date.setBackgroundColor(getResources().getColor(R.color.color2));blank2 = false;} else {et_content.setBackgroundColor(getResources().getColor(R.color.colorWhite));tv_date.setBackgroundColor(getResources().getColor(R.color.colorWhite));blank2 = true;}}});button3.setOnClickListener(new View.OnClickListener() {boolean blank3 = true;@Overridepublic void onClick(View v) {if (blank3){et_content.setBackgroundColor(getResources().getColor(R.color.color3));tv_date.setBackgroundColor(getResources().getColor(R.color.color3));blank3 = false;} else {et_content.setBackgroundColor(getResources().getColor(R.color.colorWhite));tv_date.setBackgroundColor(getResources().getColor(R.color.colorWhite));blank3 = true;}}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);}public boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {String content = et_content.getText().toString();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();if (ENTER_STATE == 0) {if (!content.equals("")) {sql = "insert into note values(" + count + ", '" + content + "' , '" + dateNum + "')";dbread.execSQL(sql);}} else {String updatesql = "update note set content='" + content + "' where _id=" + id;dbread.execSQL(updatesql);}Intent data = new Intent();setResult(2, data);finish();return true;} else {return super.onKeyDown(keyCode, event);}}}

NotesDB
package com.example.myapplication;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;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);}@Overridepublic 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\"\"" + ")";db.execSQL(sql);}@Overridepublic void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {}}