欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现记事本功能(26)

程序员文章站 2023-12-20 11:25:46
本文实例为大家分享了android实现记事本功能的具体代码,供大家参考,具体内容如下 mainactivity.java代码: package siso.s...

本文实例为大家分享了android实现记事本功能的具体代码,供大家参考,具体内容如下

mainactivity.java代码:

package siso.smartnotef.activity;

import android.app.alertdialog;
import android.content.dialoginterface;
import android.content.intent;
import android.os.bundle;
import android.support.v7.app.appcompatactivity;
import android.view.view;
import android.view.viewgroup;
import android.widget.listadapter;
import android.widget.listview;
import android.widget.textview;

import java.util.arraylist;
import java.util.list;

import siso.smartnotef.r;
import siso.smartnotef.adapter.notepadeadapter;
import siso.smartnotef.db.datahelper;
import siso.smartnotef.global.globalparams;
import siso.smartnotef.model.notepadbean;
import siso.smartnotef.model.notepadwithdatabean;
import siso.smartnotef.service.mainservice;

public class mainactivity extends appcompatactivity implements view.onclicklistener, notepadeadapter.clickfunction {

 private textview tv_add;
 private listview lv_contents;
 private list<notepadwithdatabean> notepadwithdatabeanlist;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  intent intent1 = new intent(mainactivity.this, mainservice.class);
  startservice(intent1);
  findviews();
  setlisteners();
  initview();

 }

 private void findviews() {
  tv_add = (textview) findviewbyid(r.id.tv_add);
  lv_contents = (listview) findviewbyid(r.id.lv_content);
 }

 private void setlisteners() {
  tv_add.setonclicklistener(this);
 }

 private void initview() {
  datahelper helper = new datahelper(mainactivity.this);
  notepadwithdatabeanlist = new arraylist<notepadwithdatabean>();
  list<notepadbean> notepadbeanlist = helper.getnotepadlist();
  for (int i = 0; i < notepadbeanlist.size(); i++) {
   if (0 == notepadwithdatabeanlist.size()) {
    notepadwithdatabean notepadwithdatabean = new notepadwithdatabean();
    notepadwithdatabean.setdata(notepadbeanlist.get(0).getdate());
    notepadwithdatabeanlist.add(notepadwithdatabean);
   }
   boolean flag = true;
   for (int j = 0; j < notepadwithdatabeanlist.size(); j++) {
    int date = notepadwithdatabeanlist.get(j).getdata();
    if (date == notepadbeanlist.get(i).getdate()) {
     notepadwithdatabeanlist.get(j).getnotepadbeenlist().add(notepadbeanlist.get(i));
     flag = false;
     break;
    }
   }
   if (flag) {
    notepadwithdatabean notepadwithdatabean = new notepadwithdatabean();
    notepadwithdatabean.setdata(notepadbeanlist.get(i).getdate());
    notepadwithdatabeanlist.add(notepadwithdatabean);
    notepadwithdatabeanlist.get(notepadwithdatabeanlist.size() - 1).getnotepadbeenlist().add(notepadbeanlist.get(i));
   }
  }
  notepadeadapter adapter = new notepadeadapter(mainactivity.this, notepadwithdatabeanlist, this);
  lv_contents.setadapter(adapter);
//  setlistviewheightbasedonchildren(lv_contents);
 }

 public void setlistviewheightbasedonchildren(listview listview) {
  if (listview == null) return;
  listadapter listadapter = listview.getadapter();
  if (listadapter == null) {
   // pre-condition
   return;
  }
  int totalheight = 0;
  for (int i = 0; i < listadapter.getcount(); i++) {
   view listitem = listadapter.getview(i, null, listview);
   listitem.measure(0, 0);
   totalheight += listitem.getmeasuredheight();
  }
  viewgroup.layoutparams params = listview.getlayoutparams();
  params.height = totalheight + (listview.getdividerheight() * (listadapter.getcount() - 1));
  listview.setlayoutparams(params);
 }

 @override
 public void onclick(view v) {
  switch (v.getid()) {
   case r.id.tv_add:
    intent intent = new intent();
    bundle bundle = new bundle();
    bundle.putint(globalparams.type_key, globalparams.type_add);
    intent.putextras(bundle);
    intent.setclass(mainactivity.this, addcontentactivity.class);
    startactivityforresult(intent, globalparams.add_request);
    break;
  }
 }

 @override
 protected void onactivityresult(int requestcode, int resultcode, intent data) {
  super.onactivityresult(requestcode, resultcode, data);
  switch (requestcode) {
   case globalparams.add_request:
    if (globalparams.add_result_ok == resultcode) {
     initview();
    }
    break;
  }
 }

 @override
 public void clickitem(int position, int itemposition) {
  bundle bundle = new bundle();
  bundle.putint(globalparams.type_key, globalparams.type_edit);
  bundle.putserializable(globalparams.bean_key, notepadwithdatabeanlist.get(position));
  bundle.putint(globalparams.item_position_key, itemposition);
  intent intent = new intent(this, addcontentactivity.class);
  intent.putextras(bundle);
  startactivityforresult(intent, globalparams.add_request);
 }

 @override
 public void longclickitem(final int position, final int itempostion) {
  alertdialog.builder builder = new alertdialog.builder(mainactivity.this);
  builder.setmessage("确认删除吗?");
  builder.settitle("提示");
  builder.setpositivebutton("确认", new dialoginterface.onclicklistener() {
   @override
   public void onclick(dialoginterface dialog, int which) {
    datahelper helper = new datahelper(mainactivity.this);
    helper.deletenotepad(notepadwithdatabeanlist.get(position).getnotepadbeenlist().get(itempostion).getid());
    initview();
   }
  });
  builder.setnegativebutton("取消", new dialoginterface.onclicklistener() {
   @override
   public void onclick(dialoginterface dialog, int which) {
    dialog.dismiss();
   }
  });
  builder.create().show();
 }
}

addcontentactivity.java代码:

package siso.smartnotef.activity;

import android.app.datepickerdialog;
import android.app.timepickerdialog;
import android.os.bundle;
import android.support.v7.app.appcompatactivity;
import android.view.view;
import android.widget.datepicker;
import android.widget.edittext;
import android.widget.textview;
import android.widget.timepicker;
import android.widget.toast;

import java.util.calendar;

import siso.smartnotef.r;
import siso.smartnotef.db.datahelper;
import siso.smartnotef.global.globalparams;
import siso.smartnotef.model.notepadbean;
import siso.smartnotef.model.notepadwithdatabean;

public class addcontentactivity extends appcompatactivity implements view.onclicklistener {

 private textview tv_save;
 private textview tv_date;
 private textview tv_time;
 private textview tv_cancel;
 private edittext et_content;
 private string time = "";
 private string date = "";
 private bundle bundle;
 private int type;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_add_content);
  bundle=getintent().getextras();
  type=bundle.getint(globalparams.type_key);
  findviews();
  setlisteners();
  initdate();
 }

 private void findviews() {
  et_content=(edittext)findviewbyid(r.id.et_content);
  tv_save = (textview) findviewbyid(r.id.tv_save);
  tv_date = (textview) findviewbyid(r.id.tv_date);
  tv_time = (textview) findviewbyid(r.id.tv_time);
  tv_cancel=(textview)findviewbyid(r.id.tv_cancel);
 }

 private void setlisteners() {
  tv_save.setonclicklistener(this);
  tv_date.setonclicklistener(this);
  tv_time.setonclicklistener(this);
  tv_cancel.setonclicklistener(this);
 }

 private void initdate() {
  calendar c = calendar.getinstance();
  int year=c.get(calendar.year);
  int month=c.get(calendar.month);
  int day=c.get(calendar.day_of_month);
  date=getdate(year,month,day);
  if(type==globalparams.type_edit){
   notepadwithdatabean notepadwithdatabean=(notepadwithdatabean)(bundle.getserializable(globalparams.bean_key));
   et_content.settext(notepadwithdatabean.getnotepadbeenlist().get(bundle.getint(globalparams.item_position_key)).getcontent());
   date=notepadwithdatabean.getdata()+"";
   tv_date.settext(date);
   time=notepadwithdatabean.getnotepadbeenlist().get(bundle.getint(globalparams.item_position_key)).gettime();
   tv_time.settext(time);
  }
 }

 private string getdate(int year,int month,int day){
  string date="";
  date+=year;
  if(month<9){
   date=date+"0"+(month+1);
  }else{
   date+=(month+1);
  }
  if(day<10){
   date=date+"0"+day;
  }else {
   date+=day;
  }
  return date;
 }
 @override
 public void onclick(view v) {
  switch (v.getid()) {
   case r.id.tv_save:
    if(type==globalparams.type_edit){
     update();
    }else {
     save();
    }
    break;
   case r.id.tv_date:
    selectdatedialog();
    break;
   case r.id.tv_time:
    selecttimedialog();
    break;
   case r.id.tv_cancel:
    finish();
    break;
  }
 }
 private void selectdatedialog(){
  calendar c = calendar.getinstance();
  int year=c.get(calendar.year);
  final int month=c.get(calendar.month)+1;
  int day=c.get(calendar.day_of_month);
  new datepickerdialog(addcontentactivity.this, new datepickerdialog.ondatesetlistener() {
   @override
   public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) {
    date=getdate(year,monthofyear,dayofmonth);
    tv_date.settext(date);
   }
  },year,month,day).show();
 }

 private void selecttimedialog() {
  calendar c = calendar.getinstance();
  int mhour = c.get(calendar.hour_of_day);
  int mminute = c.get(calendar.minute);
  new timepickerdialog(addcontentactivity.this,
    new timepickerdialog.ontimesetlistener() {

     @override
     public void ontimeset(timepicker view,
           int hourofday, int minute) {
      time=formattime(hourofday,minute);
      tv_time.settext(time);
     }
    }, mhour, mminute, true).show();
 }

 private string formattime(int hour,int minute){
  string time=hour+":";
  if(minute<10){
   time=time+"0"+minute;
  }else{
   time+=minute;
  }
  return time;
 }
 private void save() {
  string content = et_content.gettext().tostring();
  if ("".equals(content)) {
   toast.maketext(addcontentactivity.this, "请输入内容", toast.length_short).show();
   return;
  }
  if ("".equals(time)) {
   toast.maketext(addcontentactivity.this, "请选择时间", toast.length_short).show();
   return;
  }
  notepadbean notepadbean = new notepadbean();
  notepadbean.setcontent(content);
  notepadbean.setdate(integer.parseint(date));
  notepadbean.settime(time);
  datahelper helper = new datahelper(addcontentactivity.this);
  helper.insertdata(notepadbean);
  setresult(globalparams.add_result_ok);
  finish();
 }
 private void update(){
  datahelper helper=new datahelper(addcontentactivity.this);
  notepadwithdatabean bean=(notepadwithdatabean)(bundle.getserializable(globalparams.bean_key));
  int itemposition=bundle.getint(globalparams.item_position_key);
  helper.update(bean.getnotepadbeenlist().get(itemposition).getid(),date,time,et_content.gettext().tostring());
  setresult(globalparams.add_result_ok);
  finish();
 }
}

remindactivity.java代码:

package siso.smartnotef.activity;

import android.media.mediaplayer;
import android.media.ringtonemanager;
import android.net.uri;
import android.os.bundle;
import android.support.v7.app.appcompatactivity;
import android.view.view;
import android.widget.button;
import android.widget.textview;

import java.io.ioexception;

import siso.smartnotef.r;
import siso.smartnotef.global.globalparams;

public class remindactivity extends appcompatactivity {

 private textview tv_content;
 private button bt_confirm;
 private mediaplayer mmediaplayer;;
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_remind);
  findviews();
  setlisteners();
  bundle bundle=getintent().getextras();
  string content=bundle.getstring(globalparams.content_key);
  tv_content.settext(content);
  playsound();
 }

 private void findviews(){
  tv_content=(textview)findviewbyid(r.id.tv_content);
  bt_confirm=(button) findviewbyid(r.id.bt_confirm);
 }
 private void setlisteners(){
  bt_confirm.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    if(null!=mmediaplayer){
     mmediaplayer.stop();
     finish();
    }
   }
  });
 }

 public void playsound() {
  new thread(new runnable() {
   @override
   public void run() {
    mmediaplayer = mediaplayer.create(remindactivity.this, getsystemdefultringtoneuri());
    mmediaplayer.setlooping(true);//设置循环
    try {
     mmediaplayer.prepare();
    } catch (illegalstateexception e) {
     e.printstacktrace();
    } catch (ioexception e) {
     e.printstacktrace();
    }
    mmediaplayer.start();
   }
  }).start();

 }
 //获取系统默认铃声的uri
 private uri getsystemdefultringtoneuri() {
  return ringtonemanager.getactualdefaultringtoneuri(remindactivity.this,
    ringtonemanager.type_ringtone);
 }

}

activity_main.xml内容:

<?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:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="siso.smartnotef.activity.mainactivity">

 <relativelayout
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:background="@color/title_color"
  android:paddingleft="10dp"
  android:paddingright="10dp">
  <textview
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textcolor="@color/white"
   android:textsize="18sp"
   android:layout_centerinparent="true"
   android:text="智能记事本"/>
  <textview
   android:id="@+id/tv_add"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textcolor="@color/white"
   android:text="新增"
   android:layout_centervertical="true"
   android:layout_alignparentright="true"
   android:textsize="13sp"/>
 </relativelayout>
 <listview
  android:id="@+id/lv_content"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"></listview>
</linearlayout>

activity_add_content.xml内容:

<?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:id="@+id/activity_add_content"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="siso.smartnotef.activity.addcontentactivity">
 <relativelayout
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:background="@color/title_color"
  android:paddingleft="10dp"
  android:paddingright="10dp">
  <textview
   android:id="@+id/tv_cancel"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centervertical="true"
   android:text="取消"
   android:textcolor="@color/white"/>
  <textview
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textcolor="@color/white"
   android:textsize="18sp"
   android:layout_centerinparent="true"
   android:text="智能记事本"/>
  <textview
   android:id="@+id/tv_save"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textcolor="@color/white"
   android:text="保存"
   android:layout_centervertical="true"
   android:layout_alignparentright="true"
   />
 </relativelayout>
 <linearlayout
  android:layout_margintop="10dp"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:layout_gravity="center_horizontal">
  <textview
   android:id="@+id/tv_date"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="5dp"
   android:text="今天"/>
  <textview
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text=" -- "/>
  <textview
   android:id="@+id/tv_time"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="5dp"
   android:text="请选择时间"
   />
 </linearlayout>
 <edittext
  android:id="@+id/et_content"
  android:layout_margintop="10dp"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:inputtype="textmultiline"
  android:gravity="left|top"
  android:layout_margin="20dp"
  android:padding="10dp"
  android:hint="请输入内容"
  android:background="@drawable/edit_back"/>
</linearlayout>

activity_remind.xml内容:

<?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:background="@color/white"
 android:orientation="vertical"
 tools:context="siso.smartnotef.activity.remindactivity">

 <textview
  android:id="@+id/tv_content"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal" />

 <button
  android:id="@+id/bt_confirm"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:text="取消" />
</linearlayout>

androidmanifest.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="siso.smartnotef">
 <uses-permission android:name="android.permission.receive_boot_completed" />
 <uses-permission android:name="android.permission.system_alert_window" />

 <application
  android:allowbackup="true"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:supportsrtl="true"
  android:theme="@style/theme.appcompat.light.noactionbar">

  <activity
   android:name=".activity.mainactivity"
   android:theme="@style/theme.appcompat.light.noactionbar">
   <intent-filter>
    <action android:name="android.intent.action.main" />

    <category android:name="android.intent.category.launcher" />
   </intent-filter>
  </activity>

  <receiver android:name=".receiver.mainreceiver">
   <intent-filter>
    <action android:name="android.intent.action.boot_completed" />
   </intent-filter>
  </receiver>

  <activity android:name=".activity.addcontentactivity" />

  <service
   android:name=".service.mainservice"
   android:enabled="true"
   android:exported="true" />

  <activity android:name=".activity.remindactivity"
   ></activity>


 </application>

</manifest>

项目结构如图:

Android实现记事本功能(26)

项目运行结果如图:

Android实现记事本功能(26)

Android实现记事本功能(26)

Android实现记事本功能(26)

Android实现记事本功能(26)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: