Android课程设计日记本程序_第1页
Android课程设计日记本程序_第2页
Android课程设计日记本程序_第3页
Android课程设计日记本程序_第4页
Android课程设计日记本程序_第5页
已阅读5页,还剩23页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、大连安博教育实训基地大连实训课程设计日记本程序xxx2021/12/261工程任务概述要实现的日历除了常规的日历功能外, 还可以显示与当前日期相关的信息, 如当天气预报等信息。1.2 工程任务本工程实现在Android移动设备上,查看万年历,查询天气,记录提醒定时。方便人们生活,是人们记住做某事在预期的时间。2需求分析用户可以使用该系统实现万年历的查看,天气预报的查看,添加备忘录,查看修改备忘功能。图 1主界面用例图图图3用户通过点击桌面“万年历图标进入万年历系统,进入系统后首页会显示万年厉。显示日期为当前日期,星期几,本月中的第几周,本年中的第几周。按菜单键,有四项功能,分别是,“今日天气、

2、“指定日期,“记录/提醒和“关于,单击选项选择功能。图4 功能1.查询今日的天气,会显示天气信息,如温度,风力,穿衣指南等功能2.指定日期使主界面的显示信息定位。功能3.添加提醒信息。功能4.显示软件相关信息。3 系统设计3.1 万年历UI主界面图5 图6 主界面,主要显示日历信息,有记录的在数字前面显示“*,可以通过点击灰色的数字活着两个按钮切换月份。本页面有4个菜单项。今日天气图7有一个EditView,一个Button等,能够清晰的显示当天的气温情况和少许的提醒信息,使用户一目了然。3.3 万年历UI指定日期图8此界面是一个对话框,包含一个DatePicker组件和确定取消按钮,比拟清晰

3、使用。3.4 万年历UI添加提醒图9 这是一个ListActivity,有一个菜单项。3.5 万年历UI添加提醒具体页面图10 这是添加提醒界面的功能具体页面,添加提醒的具体信息。3.5 日记本UI修改,删除提醒记录图113.6 日记本UI关于图124数据库设计我们的系统采用的是SQLite嵌入式数据库,在这个数据库中有且只有一个表t_emp表。 提醒主题 integer内容 varchar提醒日 varchar提醒时间 remind_time震动 boolean铃声 boolean数据库名称diary完成日期版本设计者审核者2021-12-271快乐盛表名t_records实体名称万年历主键

4、_id索引表名称和索引字段名称序号工程名称工程说明类型宽度位数属性备注1_id编号integerP、N、I2title主题varcharN3content内容integerN4record_date日期dateN5remind_time提醒时间dateN6shake震动booleanring铃声boolean提示:U:唯一(Unique)P:主键(Primary)N:非空(Not Null) F:外键(Foreign)D:默认(Default)I:自增长(auto increment)5系统实现代码解析AndroidManifest.xml是系统中的配置文件,我们需要将系统中用到的Androi

5、d组件全都在这个文件中注册,其中ActivityMain是启动屏幕显示的Activtiy。 5.2.1首先,我们将要介绍的是系统的启动界面Main.java,亦即登录界面的代码。代码清单2.1 Calendar.java主体:public class Main extends Activity implements OnDateChangedListener/变量定义Overrideprotected void onCreate(Bundle savedInstanceState) / TODO Auto-generated method stubsuper.onCreate(savedIns

6、tanceState);LinearLayout mainLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);setContentView(mainLayout); calendarView = new CalendarView(this);mainLayout.addView(calendarView);Overridepublic boolean onCreateOptionsMenu(Menu menu) /四个菜单项Overridepublic boolean onOptionsItemSel

7、ected(MenuItem item) switch (item.getItemId() case MiToday:Today();break;case MiMyDate:MyDate();break;case MiRecordRemind:RecordRemind();break;case MiAbout:About();break;return true;private void Today() Intent intent = new Intent(Main.this, Today.class);this.startActivity(intent);public void About()

8、Intent intent = new Intent(Main.this, About.class);this.startActivity(intent);public void RecordRemind()Intent intent = new Intent(Main.this, AllRecord.class);intent.putExtra(year, calendarView.ce.grid.currentYear);intent.putExtra(month, calendarView.ce.grid.currentMonth);intent.putExtra(day, calend

9、arView.ce.grid.currentDay1);this.startActivity(intent);public void MyDate()/指定日期Overridepublic void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) SimpleDateFormat sdf = new SimpleDateFormat(yyyy年M月d日);java.util.Calendar calendar = java.util.Calendar.getInstance();calendar.

10、set(year, monthOfYear, dayOfMonth);if (tvDate != null)tvDate.setText(sdf.format(calendar.getTime();elseadMyDate.setTitle(sdf.format(calendar.getTime();Calendar calendar1 = Calendar.getInstance();if (calendar1.get(Calendar.YEAR) = year& calendar1.get(Calendar.MONTH) = monthOfYear& calendar1.get(Calen

11、dar.DATE) = dayOfMonth)if (tvDate != null)tvDate.setText(tvDate.getText() + (今天);elseadMyDate.setTitle(sdf.format(calendar.getTime() + (今天);if (tvLunarDate = null)return;此类用于创立系统主界面。onCreateOptionsMenu。其中main布局如下代码。代码清单2.2 5.2.2现在,让我们跟随用户操作来了解下一个段代码,即用户查看当天天气代码清单3 Today.java。访问webService及显示天气信息。代码清单

12、3.1 访问webService及显示天气信息。private void parseWeather(SoapObject detail)throws UnsupportedEncodingException String str = new String18;int N = str.length;StringBuffer buffer = new StringBuffer();for (int i = 0; i N; i+) stri = detail.getProperty(i).toString();buffer.append(i).append(stri).append(n);Strin

13、g s = new String();s = detail.getProperty(8).toString();String temp = detail.getProperty(8).toString();StringBuffer strbuffer = new StringBuffer();System.out.println(str);for (int i = 0; i+) if (temp.charAt(i) = .) break; else strbuffer.append(temp.charAt(i);int num = Integer.parseInt(strbuffer.toSt

14、ring();flag = false;imageView.setImageResource(R.drawable.a_0 + num);String date = detail.getProperty(6).toString();weatherToday = 今天: + date.split( )0;weatherToday = weatherToday + n天气: + date.split( )1;weatherToday = weatherToday + n气温:+ detail.getProperty(5).toString();weatherToday = weatherToday

15、 + n风力:+ detail.getProperty(7).toString() + n+ str11.split(n)0;weather.setText(weatherToday);class MyThread extends Thread MyHandler handler = new MyHandler();Overridepublic void run() try sleep(1000); catch (Exception e) e.printStackTrace();Message message = new Message();handler.sendMessage(messag

16、e);class MyHandler extends Handler Overridepublic void handleMessage(Message msg) try progressDialog();/ 下拉框或列表String city = editText.getText().toString();getWeather(city);dialog.cancel(); catch (Exception e) e.printStackTrace();5.2.3现在,让我们跟随用户操作来了解下一个段代码,即指定日期。代码清单4 Main.java中的MyDate。public void My

17、Date()myDateLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.mydate, null);dpSelectDate = (DatePicker) myDateLayout.findViewById(R.id.dpSelectDate);/ Create a builderbuilder = new AlertDialog.Builder(this);builder.setTitle(指定日期);myDateLayout = (LinearLayout) getLayoutInflater().inflate(R

18、.layout.mydate, null);dpSelectDate = (DatePicker) myDateLayout.findViewById(R.id.dpSelectDate);tvDate = (TextView) myDateLayout.findViewById(R.id.tvDate);tvLunarDate = (TextView) myDateLayout.findViewById(R.id.tvLunarDate);dpSelectDate.init(calendarView.ce.grid.currentYear,calendarView.ce.grid.curre

19、ntMonth,calendarView.ce.grid.currentDay, this);builder.setView(myDateLayout);builder.setPositiveButton(确定, new DialogInterface.OnClickListener() Overridepublic void onClick(DialogInterface dialog, int which) calendarView.ce.grid.currentYear = dpSelectDate.getYear();calendarView.ce.grid.currentMonth

20、= dpSelectDate.getMonth();calendarView.ce.grid.currentDay = dpSelectDate.getDayOfMonth();calendarView.invalidate(););builder.setNegativeButton(取消, null);builder.setIcon(R.drawable.calendar_small);adMyDate = builder.create();onDateChanged(dpSelectDate, dpSelectDate.getYear(), dpSelectDate.getMonth(),

21、 dpSelectDate.getDayOfMonth();adMyDate.show();5.2.4现在,让我们跟随用户操作来了解下一个段代码,即添加提醒。代码清单4.1显示listview中的list:protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);year = getIntent().getExtras().getInt(year);month = getIntent().getExtras().getInt(month);day = getIntent().get

22、Extras().getInt(day);Cursor cursor = Grid.dbService.query(year + - + month + - + day);if (recordArray = null)recordArray = new ArrayList();if (arrayAdapter = null)arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, recordArray);elsearrayAdapter.clear();idList.clear();while (cur

23、sor.moveToNext() arrayAdapter.add(cursor.getString(1);idList.add(cursor.getInt(0);SimpleDateFormat sdf = new SimpleDateFormat(yyyy年M月d日);java.util.Calendar calendar = java.util.Calendar.getInstance();calendar.set(year, month, day);setTitle(sdf.format(calendar.getTime();setListAdapter(arrayAdapter);m

24、yListActivity = null;myListActivity = this;代码清单4.2 list单击事件:protected void onListItemClick(ListView l, View v, int position, long id) super.onListItemClick(l, v, position, id);final int tempposition = position;l.setOnItemClickListener(new AdapterView.OnItemClickListener() Overridepublic void onItemC

25、lick(AdapterView arg0, View arg1, int arg2,long arg3) CheckDialog(arg2););代码清单4.3 修改删除对话框:public void CheckDialog(long position)final int tempposition = (int)position;builder = new AlertDialog.Builder(this);builder.setTitle(修改或删除用户信息);builder.setItems(new String 修改信息, 删除信息 ,new OnClickListener() Ove

26、rridepublic void onClick(DialogInterface dialog,int which) switch (which) case 0:startEditRecordActivity(tempposition);break;case 1:Delete(tempposition);break;);builder.create().show();代码清单4.4 添加提醒的具体Activity即,其具体布局addto.xml如下:5.2.5现在,让我们跟随用户操作来了解下一个段代码,即关于。代码清单5 about.xml:CalendarView,万年历主界面的主体。Cal

27、endarView用到画布,分成画边框,画网格,画文字,显示信息等代码清单6.1 CalendarView所用到一个接口CalendarElement.java :public interface CalendarElement public void draw(Canvas canvas);代码清单6.2 CalendarView画边框 Border.java:public void draw(Canvas canvas)float left = borderMargin;float top = borderMargin;float right = view.getMeasuredWidth

28、() - left;float bottom = view.getMeasuredHeight() - top;canvas.drawLine(left, top, right, top, paint);canvas.drawLine(right, top, right, bottom, paint);canvas.drawLine(right, bottom, left, bottom, paint);canvas.drawLine(left, bottom, left, top, paint);代码清单6.3 CalendarView画星期 Week.java:public void dr

29、aw(Canvas canvas)float left = borderMargin;float top = borderMargin;float everyWeekWidth = (view.getMeasuredWidth() - borderMargin * 2)/ 7;float everyWeekHeight = everyWeekWidth;paint.setFakeBoldText(true);for (int i = 0; i weekNames.length; i+)if(i = 0 | i = weekNames.length - 1)paint.setColor(sund

30、aySaturdayColor);elsepaint.setColor(weekNameColor);left = borderMargin + everyWeekWidth * i+ (everyWeekWidth - paint.measureText(weekNamesi) / 2;canvas.drawText(weekNamesi, left, top + paint.getTextSize()+weekNameMargin, paint);代码清单6.4 CalendarView画日期,网格 Grid.java:paint.setColor(innerGridColor);canv

31、as.drawLine(left, top, left + view.getMeasuredWidth()- borderMargin * 2, top, paint);/ 画横线for (int i = 1; i 6; i+) canvas.drawLine(left, top + (cellHeight) * i, left + calendarWidth,top + (cellHeight) * i, paint);/ 画竖线for (int i = 1; i 7; i+) canvas.drawLine(left + cellWidth * i, top, left + cellWid

32、th * i,view.getMeasuredHeight() - borderMargin, paint);/ 画日期calculateDays();java.util.Calendar calendar = java.util.Calendar.getInstance();int day = calendar.get(calendar.DATE);int myYear = calendar.get(calendar.YEAR), myMonth = calendar.get(calendar.MONTH);calendar.set(calendar.get(calendar.YEAR),

33、calendar.get(calendar.MONTH),1);int week = calendar.get(calendar.DAY_OF_WEEK);int todayIndex = week + day - 2;boolean today = false;if (currentDayIndex = -1) currentDayIndex = todayIndex;boolean flag = false;getRecordDays();for (int i = 0; i 20) currentMonth+;if (currentMonth = 12) currentMonth = 0;

34、currentYear+;view.invalidate();/ 上月else currentMonth-;if (currentMonth = -1) currentMonth = 11;currentYear-;view.invalidate();currentDay = Integer.parseInt(text);currentDay1 = currentDay;cellX = -1;cellY = -1;break; else paint.setTextSize(currentDaySize);flag = true;Bitmap bitmap = BitmapFactory.dec

35、odeResource(activity.getResources(), R.drawable.day);Rect src = new Rect();src.left = 0;src.top = 0;src.right = bitmap.getWidth();src.bottom = bitmap.getHeight();canvas.drawBitmap(bitmap, src, dst, paint);paint.setColor(currentDayColor);currentCol = col;currentRow = row;currentDay = Integer.parseInt

36、(text);currentDay1 = currentDay;updateMsg(today);canvas.drawText(myText, textLeft, textTop, paint);(1).日历上面的显示日期布局即main.xml上面已经说到,方法如下updateMsg(today):private void updateMsg(boolean today) String monthName = monthNamescurrentMonth;String dateString = ;SimpleDateFormat sdf = new SimpleDateFormat(yyyy

37、年M月d日);java.util.Calendar calendar = java.util.Calendar.getInstance();calendar.set(currentYear, currentMonth, currentDay);dateString = sdf.format(calendar.getTime();String lunarStr = ;monthName += 本月第 + calendar.get(java.util.Calendar.WEEK_OF_MONTH)+ 周 + 本年第+ calendar.get(java.util.Calendar.WEEK_OF_

38、YEAR) + 周;tvMsg1.setText(monthName);if (today)dateString += (今天);tvMsg2.setText(dateString);button1.setOnClickListener(new View.OnClickListener() Overridepublic void onClick(View v) currentMonth-;if (currentMonth = -1) currentMonth = 11;currentYear-;if (currentMonth = 12) currentMonth = 0;currentYea

39、r+;view.invalidate(););button2.setOnClickListener(new View.OnClickListener() Overridepublic void onClick(View v) currentMonth+;if (currentMonth = 12) currentMonth = 0;currentYear+;view.invalidate(););(2).连接数据库,查询是否有记录:private void getRecordDays() int beginIndex = 8;int endIndex = 7;int beginDayIndex

40、 = 0;if (currentMonth 9) beginIndex = 9;endIndex = 8;String sql = select substr(record_date, + beginIndex+ ) from t_records where substr(record_date, 1, + endIndex+ )= + currentYear + - + currentMonth+ - group by substr(record_date, 1);for (int i = 0; i recordDays.length; i+)recordDaysi = false;for

41、(int i = 0; i = + minDay+ group by substr(record_date, 1);cursor = dbService.execSQL(sql);while (cursor.moveToNext() int day = cursor.getInt(0);recordDaysday - minDay = true;if (daysdays.length - 1.startsWith(*) int nextYear = currentYear, nextMonth = currentMonth + 1;if (nextMonth = 12) nextMonth =

42、 0;nextYear+;int maxDay = Integer.parseInt(daysdays.length - 1.substring(1);sql = select substr(record_date, + beginIndex+ ) from t_records where substr(record_date, 1, + endIndex + )= + nextYear + - + nextMonth+ - and cast(substr(record_date, + beginIndex+ ) as int) = + maxDay+ group by substr(reco

43、rd_date, 1);cursor = dbService.execSQL(sql);while (cursor.moveToNext() int day = cursor.getInt(0);recordDaysdays.length - (maxDay - day) - 1 = true;(3).具体计算某一天是所在年的第几周,星期几具体方法是:private int getMonthDays(int year, int month);private void calculateDays();代码清单6.5 Calendar类 主要方法:public Calendar(Activity

44、activity, View view)super(activity,view);elements.add(new Border(activity, view);elements.add(new Week(activity, view);grid = new Grid(activity, view);elements.add(grid);5.2.7现在,让我们了解万年历中的数据库:public class DBService extends SQLiteOpenHelperprivate final static int DATABASE_VERSION = 4;private final s

45、tatic String DATABASE_NAME = calendar.db;Overridepublic void onCreate(SQLiteDatabase db)String sql = CREATE TABLE t_records (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,+ title VARCHAR(30) NOT NULL, content TEXT, record_date DATE NOT NULL,remind_time TIME,+ remind BOOLEAN,shake BOOLEAN,ring BOOLEA

46、N)+ ;CREATE INDEX unique_title ON t_records (title);+ CREATE INDEX remind_time_index ON t_records (remind_time);+ CREATE INDEX record_date_index ON t_records (record_date);+ CREATE INDEX remind_index ON t_records (remind);db.execSQL(sql);public DBService(Context context)super(context, DATABASE_NAME,

47、 null, DATABASE_VERSION);public Cursor execSQL(String sql)SQLiteDatabase db = this.getReadableDatabase();Cursor cursor = db.rawQuery(sql, null);return cursor;Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)String sql = drop table if exists t_records;db.execSQL(sql);sq

48、l = CREATE TABLE t_records (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,+ title VARCHAR(30) NOT NULL, content TEXT, record_date DATE NOT NULL,remind_time TIME,+ remind BOOLEAN,shake BOOLEAN,ring BOOLEAN)+ ;CREATE INDEX unique_title ON t_records (title);+ CREATE INDEX remind_time_index ON t_records

49、 (remind_time);+ CREATE INDEX record_date_index ON t_records (record_date);+ CREATE INDEX remind_index ON t_records (remind);db.execSQL(sql);public void insertRecord(String title, String content, String recordDate)insertRecord(title, content, recordDate, null, false, false);public void insertRecord(

50、String title, String content, String recordDate,String remindTime, boolean shake, boolean ring)try String sql = ;String remind = false;if (remindTime != null)remind = trueelseremindTime = 0:0:0;sql = insert into t_records(title, content, record_date,remind_time, remind, shake, ring) values(+ title+

51、,+ content+ ,+ recordDate+ ,+ remindTime+ ,+ remind+ ,+ shake+ ,+ ring + );SQLiteDatabase db = this.getWritableDatabase();db.execSQL(sql);catch (Exception e)Log.d(error, e.getMessage();public void deleteRecord(int id)String sql = delete from t_records where id = + id;SQLiteDatabase db = this.getWrit

52、ableDatabase();db.execSQL(sql);public void updateRecord(int id, String title, String content,String remindTime, boolean shake, boolean ring)tryString sql = ;String remind = false;if (remindTime != null)remind = true;elseremindTime = 0:0:0;sql = update t_records set title= + title + , content=+ conte

53、nt + ,remind_time= + remindTime + , remind=+ remind + ,shake= + shake + , ring= + ring+ where id= + id;SQLiteDatabase db = this.getWritableDatabase();db.execSQL(sql);catch (Exception e)Log.d(updateRecord, e.getMessage();public int getMaxId(String date)SQLiteDatabase db = this.getReadableDatabase();Cursor cursor = db.rawQuery(select max(id) from t_records where record_date= + date+ , null);cursor.moveToFirst();return cursor.getInt(0);public Cursor query(String date)SQLiteDatabase db = this.getReadableDatabase();Cursor cursor = db.rawQuery(s

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论