备忘录

来源:互联网 发布:群发短信软件哪个好 编辑:程序博客网 时间:2024/06/05 14:48

        一直都想把自己写过的安卓开发小程序整理一下,做成文档,但是一直没有机会,刚好,最近都不是很忙,于是今天就将之前花了好几天才完成的备忘录整理下,跟大家分享分享。要完成这个程序,主要从以下几步入手:

1、首先写一个类继承辅助类SQLiteOpenHelper,在里面重写Oncreate方法和update方法(一般情况下只有当数据库的版本发生改变时才会调用该方法,本程序中没有使用到该方法)

2、在相继写以下几个类,都继承Activity

    (1)MainActivity ,在该类中显示备忘录的主页布局

        主页面布局:一个按钮(Button),一个ListView

    (2)AddActivity(代码中的Add类) ,在该页中显示备忘录点击添加按钮后显示的页面的布局

        添加页面布局:三个编辑框(EditView)(题目、时间、内容)、两个按钮(Button)(保存、取消)

    (3)LookActivity(代码中的Look类),在该页中显示点击ListView条目(每一条备忘录里的信息)后显示的产看页面的布局

        查看页面布局:三个编辑框(分别显示题目、时间、内容),设置成不可编辑

    (4)MotifyActivity(代码中的Motify类),在该页中显示长按ListView条目后,进入的修改页面的布局

        与上面查看页面相似,多一个按钮,可以编辑。

    (5)还要单独设置一个布局,用来表示一条显示在ListView列表里的消息     

3、在长按ListView条目时出现提示消息(删除/修改),

        (1)点击删除:就直接从数据库中删除该条信息

    (2)点击修改:就进入到修改页面

4、在添加页面里,点击取消按钮弹出提示消息(保存/不保存)

    (1)点击不保存:直接返回到主页面

    (2)点击保存:将该条信息插入到数据库中,然后返回到主页面

5、在该项目的配置文件里面,将上面的(AddActivity、LookActivity、MotifyActivity类配置一下)

    <activityandroid:name="AddActivity"></activity>

    <activityandroid:name="LookActivity"></activity>

    <activityandroid:name="MotifyActivity"></activity>

6、代码部分:

<span style="font-size:12px;"><------------------JAVA代码部分-------------->/*主函程序*/package com.example.memo;/* *主页面 *要求: *1、显示已经存在的备忘录信息 *2、有个添加按钮,点击添加按钮可以跳到编辑页面 *3、点击条目实现查看信息 *4、长按条目跳出提示框,是否删除条目。 *   */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.content.DialogInterface;import android.content.Intent;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;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;public class MainActivity extends Activity {private MySqlDb mdb = null;private SQLiteDatabase db = null;ListView lv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.home_page);// 创建数据中的表mdb = new MySqlDb(MainActivity.this);db = mdb.getReadableDatabase();lv = (ListView) findViewById(R.id.ListView1);fresh();// 点击条目lv.setOnItemClickListener(new OnItemClickListener() {@SuppressWarnings("unchecked")@Overridepublic void onItemClick(AdapterView<?> parent, View v,int position, long id) {// 找到该条目,并将其放在map中。Map<String, Object> map = (Map<String, Object>) parent.getItemAtPosition(position);// 传输该条目的IdIntent intent = new Intent();intent.putExtra("id", map.get("m_id").toString());intent.setClass(MainActivity.this, LookMemo.class);startActivity(intent);}});// 长按条目lv.setOnItemLongClickListener(new OnItemLongClickListener() {String lid;@SuppressWarnings("unchecked")@Overridepublic boolean onItemLongClick(AdapterView<?> parent, View v,int position, long id) {Map<String, Object> map = (Map<String, Object>) parent.getItemAtPosition(position);lid = map.get("m_id").toString();// 带按钮的提示框new AlertDialog.Builder(MainActivity.this).setTitle("提示消息").setPositiveButton("修改",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {Intent intent = new Intent();intent.putExtra("id", lid);intent.setClass(MainActivity.this,Motify.class);startActivity(intent);}}).setNegativeButton("删除",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0,int arg1) {delete(lid);fresh();}}).show();return false;}});// 点击按钮Button btn = (Button) findViewById(R.id.add);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setClass(MainActivity.this, Add.class);startActivity(intent);}});}// 重启该页面,刷新该页面的数据public void onRestart() {super.onRestart();fresh();}// 刷新(将数据库中的信息在主页面的ListView列表中通过适配器显示出来)public void fresh() {SimpleAdapter sa = new SimpleAdapter(MainActivity.this, getList(),R.layout.items, new String[] { "m_title" },new int[] { R.id.tv });lv.setAdapter(sa);}// 获得数据库中的数据public List<Map<String, Object>> getList() {List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();int count = 0;Cursor cs = db.query("Memo", new String[] { "m_id", "m_title","m_time", "m_context" }, null, null, null, null, null);while (cs.moveToNext()) {int m_id = cs.getInt(cs.getColumnIndex("m_id"));if (m_id > count) {count = m_id;Map<String, Object> map = new HashMap<String, Object>();map.put("m_title", cs.getString(cs.getColumnIndex("m_title")));map.put("m_time", cs.getString(cs.getColumnIndex("m_time")));map.put("m_context",cs.getString(cs.getColumnIndex("m_context")));map.put("m_id", cs.getString(cs.getColumnIndex("m_id")));list.add(map);}}return list;}// 删除数据库中的数据public void delete(String id) {String[] id1 = { id };db.delete("Memo", "m_id=?", id1);}}/*添加信息的程序*/package com.example.memo;/* * 功能:添加一条备忘录信息、 * 要求:点击保存就插入到数据裤中,否则就回到主页面 */import android.app.Activity;import android.app.AlertDialog;import android.content.ContentValues;import android.content.DialogInterface;import android.content.Intent;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class Add extends Activity {private MySqlDb mdb = null;private SQLiteDatabase db = null;private EditText edtitle;private EditText edtime;private EditText edcontext;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.addpage);mdb = new MySqlDb(Add.this);db = mdb.getReadableDatabase();Button[] btn = new Button[2];btn[0] = (Button) findViewById(R.id.btnsave);btn[1] = (Button) findViewById(R.id.btncancle);for (int i = 0; i < btn.length; i++) {btn[i].setOnClickListener(new OnClickListener() {  //按钮点击事件@Overridepublic void onClick(View v) {Button bt = (Button) v;String str = bt.getText().toString();     //获得按钮上的信息edtitle = (EditText) findViewById(R.id.edtitle);edtime = (EditText) findViewById(R.id.edtime);edcontext = (EditText) findViewById(R.id.edcontext);if (str.equals("保存")) {/* * 如果点击的是保存按钮,就将信息插入到数据库。并跳回主页面 */String title = edtitle.getText().toString();String time = edtime.getText().toString();String context = edcontext.getText().toString();sava(db, title, time, context);   // 将信息插入到数据库   Intent intent = new Intent();intent.setClass(Add.this, MainActivity.class);startActivity(intent);   //跳回主页面} else {/* * 如果点击的不是保存按钮,就弹出提示框:是否需要保存信息,点击保存,就将信息插入到数据库, * 否则就跳回主页面 */new AlertDialog.Builder(Add.this).setTitle("信息").setMessage("是否需要保存").setPositiveButton("保存",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int which) {String title = edtitle.getText().toString();String time = edtime.getText().toString();String context = edcontext.getText().toString();sava(db, title, time, context);Intent intent = new Intent();intent.setClass(Add.this,MainActivity.class);startActivity(intent);}}).setNegativeButton("不保存",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int arg1) {Intent intent = new Intent();intent.setClass(Add.this,MainActivity.class);startActivity(intent);dialog.cancel();}}).show();}}});}}/* * 功能:向数据库中插入信息 */public void sava(SQLiteDatabase db, String title, String time,String context) {ContentValues cv = new ContentValues();cv.put("m_title", title);cv.put("m_time", time);cv.put("m_context", context);db.insert("Memo", null, cv);}}/*修改信息的程序*/package com.example.memo;/* * 功能:修改信息 * 要求:能够修改信息,点击保存按钮能够将信息插入到数据库, */import android.app.Activity;import android.content.ContentValues;import android.content.Intent;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class Motify extends Activity {private MySqlDb mdb = null;private SQLiteDatabase db = null;private EditText edtitle;private EditText edtime;private EditText edcontext;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.motify);mdb = new MySqlDb(Motify.this);db = mdb.getReadableDatabase();// 接受数据(id)Intent intent = getIntent();String id = intent.getStringExtra("id");edtitle = (EditText) findViewById(R.id.mtftitle);edtime = (EditText) findViewById(R.id.mtftime);edcontext = (EditText) findViewById(R.id.mtfcontext);//查询m_id与id相等的数据,并将他显示在当前页面上Cursor cs = db.query("Memo", new String[] { "m_id", "m_title","m_time", "m_context" }, "m_id=?", new String[] { id }, null,null, null);while (cs.moveToNext()) {edtitle.setText(cs.getString(cs.getColumnIndex("m_title")));edtime.setText(cs.getString(cs.getColumnIndex("m_time")));edcontext.setText(cs.getString(cs.getColumnIndex("m_context")));}//点击按钮将数据保存到数据库中Button btn = (Button) findViewById(R.id.btnmtfsave);btn.setOnClickListener(new OnClickListener() {// 接受数据(id)Intent intent = getIntent();String id = intent.getStringExtra("id");@Overridepublic void onClick(View v) {EditText edtitle = (EditText) findViewById(R.id.mtftitle);EditText edtime = (EditText) findViewById(R.id.mtftime);EditText edcontext = (EditText) findViewById(R.id.mtfcontext);String edt = edtitle.getText().toString();String edttime = edtime.getText().toString();String edc = edcontext.getText().toString();updata(db, edt, edttime, edc, id);  //更新信息Intent intent = new Intent();intent.setClass(Motify.this, MainActivity.class);startActivity(intent);   //跳回主页面}});}// 跟新数据库中的数据public void updata(SQLiteDatabase db, String edt, String edttime,String edc, String id) {ContentValues cv = new ContentValues();cv.put("m_title", edt);cv.put("m_time", edttime);cv.put("m_context", edc);db.update("Memo", cv, "m_id=?", new String[] { id });}}/*查看信息的程序*/package com.example.memo;/* * 功能:查看备忘录信息 * 要求:只能查看信息,不能修改信息 */import android.app.Activity;import android.content.Intent;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.widget.EditText;public class LookMemo extends Activity{private MySqlDb mdb = null;private SQLiteDatabase db =null;private EditText edtitle;private EditText edtime;private EditText edcontext;protected void onCreate(android.os.Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.fragment_main);mdb = new MySqlDb(LookMemo.this);db = mdb.getReadableDatabase(); //接受数据(id)Intent intent = getIntent();String id = intent.getStringExtra("id"); edtitle = (EditText)findViewById(R.id.title); edtime = (EditText)findViewById(R.id.time); edcontext = (EditText)findViewById(R.id.context); //查询id号为制定id的条目Cursor cs = db.query("Memo", new String[]{"m_id","m_title","m_time","m_context"}, "m_id=?", new String[]{id}, null, null, null);while(cs.moveToNext()){//将制定id的条目从数据中找出,将其内容在另一个页面里显示出来edtitle.setText(cs.getString(cs.getColumnIndex("m_title")));edtime.setText(cs.getString(cs.getColumnIndex("m_time")));edcontext.setText(cs.getString(cs.getColumnIndex("m_context")));}cs.close();db.close();mdb.close();};}/*数据库辅助类的程序*/package com.example.memo;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class MySqlDb extends SQLiteOpenHelper {public MySqlDb(Context context) {super(context, "Memo", null, 1);}@SuppressWarnings("unused")@Overridepublic void onCreate(SQLiteDatabase db) {int m_id;String m_title;String m_time;String m_context;String sql = "create table Memo(m_id integer primary key,"+ "m_title varchar(20),"+ "m_time varchar(20),"+ "m_context varchar(400))";db.execSQL(sql);}@Overridepublic void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {</span><pre name="code" class="html"><span style="font-size:12px;"><--------------------XML代码部分—————><!——主页面部分——><?xml version="1.0" encoding="utf-8"?><GridLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"        android:background="#90F7F7"    android:columnCount="1"    android:rowCount="1" >    <Button        android:id="@+id/add"        android:layout_width="fill_parent"        android:layout_height="60dp"        android:layout_column="0"        android:layout_row="0"        android:layout_marginBottom="164dp"        android:text="@string/add"         />    <ListView        android:id="@+id/ListView1"        android:layout_width="match_parent"        android:layout_height="624dp"        android:layout_column="0"        android:layout_row="0"        android:fadeScrollbars="false"        android:layout_gravity="left|top"        android:layout_marginTop="60dp"        android:clickable="true"         >    </ListView></GridLayout><!——添加页面部分——><?xml version="1.0" encoding="utf-8"?><GridLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#BBFFFF"    android:columnCount="7"    android:rowCount="3" >    <EditText        android:id="@+id/edtitle"        android:layout_width="280dp"        android:layout_height="60dp"        android:layout_column="0"        android:layout_columnSpan="5"        android:layout_row="0"        android:hint="@string/title" />    <requestFocus />    <Button        android:id="@+id/btnsave"        android:layout_width="80dp"        android:layout_height="60dp"        android:layout_column="5"        android:layout_columnSpan="2"        android:layout_marginRight="0dp"        android:layout_row="0"        android:text="@string/save" />    <EditText        android:id="@+id/edtime"        android:layout_width="280dp"        android:layout_height="60dp"        android:layout_column="4"        android:layout_row="1"        android:hint="@string/time" />    <Button        android:id="@+id/btncancle"        android:layout_width="80dp"        android:layout_height="60dp"        android:layout_column="5"        android:layout_columnSpan="2"        android:layout_row="1"        android:text="@string/cancle" />    <EditText        android:id="@+id/edcontext"        android:layout_width="fill_parent"        android:layout_height="449dp"        android:layout_column="0"        android:layout_columnSpan="7"        android:layout_row="2"        android:singleLine="false"        android:gravity="top"        android:hint="@string/context" /></GridLayout><!——修改面部分——><?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"     android:background="#90F7F7">    <Button        android:id="@+id/btnmtfsave"        android:layout_width="fill_parent"        android:layout_height="50dp"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        android:layout_alignParentRight="true"        android:text="@string/mtfsave" />    <EditText        android:id="@+id/mtftitle"        android:layout_width="fill_parent"        android:layout_height="50dp"        android:layout_alignParentLeft="true"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:ems="10"        android:text="@string/mtftitle" />    <EditText        android:id="@+id/mtfcontext"        android:layout_width="fill_parent"        android:layout_height="320dp"        android:layout_above="@+id/btnmtfsave"        android:layout_alignParentLeft="true"        android:layout_alignParentRight="true"        android:layout_below="@+id/mtftime"        android:gravity="top"        android:inputType="textMultiLine"        android:text="@string/mtfcontext" />    <EditText        android:id="@+id/mtftime"        android:layout_width="fill_parent"        android:layout_height="50dp"        android:layout_alignParentLeft="true"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:layout_marginTop="46dp"        android:ems="10"        android:text="@string/mtftime" >        <requestFocus />    </EditText></RelativeLayout><!——查看面部分——><RelativeLayout 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:background="#90F7F7"    tools:context="com.example.memo.MainActivity$PlaceholderFragment" >       <EditText        android:id="@+id/title"        android:layout_width="fill_parent"        android:layout_height="60dp"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:inputType="text"        android:focusable="false"        android:ems="10" />        <requestFocus />    <EditText        android:id="@+id/time"        android:layout_width="fill_parent"        android:layout_height="60dp"        android:layout_alignParentLeft="true"        android:layout_below="@+id/title"        android:inputType="datetime"        android:focusable="false"        android:ems="10" >    </EditText>    <EditText        android:id="@+id/context"        android:layout_width="fill_parent"        android:layout_height="320dp"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        android:layout_below="@+id/time"        android:gravity="top"        android:inputType="textMultiLine"        android:focusable="false"        android:singleLine="false" />    </RelativeLayout><!——item条目面部分——><?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" >      <!-- ListView元素 -->    <TextView        android:id="@+id/tv"        android:layout_width="fill_parent"        android:layout_height="45dp"        android:ellipsize="end"        android:textSize="20sp" /></LinearLayout></span>

}}

<span style="font-size:12px;"><!-----AndroidManifest.XML文件------><?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.memo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="14"        android:targetSdkVersion="19" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.memo.MainActivity"            android:launchMode="singleTask"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>         <activity              android:name="Add"             android:launchMode="singleTask"></activity>         <activity              android:name="LookMemo"              android:launchMode="singleTask"></activity>         <activity              android:name="Motify"             android:launchMode="singleTask"></activity>    </application></manifest></span>

<span style="font-size:12px;"><!-----Strings.XML部分----------><?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">备忘录</string>    <string name="hello_world">Hello world!</string>    <string name="action_settings">Settings</string>         <string name="title">请输入标题</string>    <string name="time">请输入时间</string>    <string name="context">请输入内容</string>    <string name="add">添加</string>    <string name="save">保存</string>    <string name="cancle">取消</string>    <string name="delete">删除</string>    <string name="mtfsave">保存</string>    <string name="mtftitle"></string>    <string name="mtftime"></string>    <string name="mtfcontext"></string></resources></span>



0 0
原创粉丝点击