当前位置: 移动技术网 > 移动技术>移动开发>Android > Android实现记事本功能

Android实现记事本功能

2019年07月24日  | 移动技术网移动技术  | 我要评论

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

实现功能

1、文本数据的存储

2、图片数据存储

3、视频数据存储

4、自定义的adapter

5、sqlite的创建

6、数据listview列表的显示

demo地址

界面布局

<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" >

 <linearlayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal" >

 <button
 android:id="@+id/text"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="文字" />

 <button
 android:id="@+id/img"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="图文" />

 <button
 android:id="@+id/video"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="视频" />
 </linearlayout>

 <listview
 android:id="@+id/list"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content" >
 </listview>

</linearlayout>

数据库创建

public class notesdb extends sqliteopenhelper {

 public static final string table_name = "notes";
 public static final string content = "content";
 public static final string path = "path";
 public static final string video = "video";
 public static final string id = "_id";
 public static final string time = "time";

 public notesdb(context context) {
 super(context, "notes", null, 1);
 }
 @override
 public void oncreate(sqlitedatabase db) {
 db.execsql("create table " + table_name + " (" + id
 + " integer primary key autoincrement," + content
 + " text not null," + path + " text not null," + video
 + " text not null," + time + " text not null)");
 }
 @override
 public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {}
}

数据的加载

public class addcontent extends activity implements onclicklistener {

 private string val;
 private button savebtn, deletebtn;
 private edittext ettext;
 private imageview c_img;
 private videoview v_video;
 private notesdb notesdb;
 private sqlitedatabase dbwriter;
 private file phonefile, videofile;

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.addcontent);
 //主界面点击button传递过来的数据
 val = getintent().getstringextra("flag");

 savebtn = (button) findviewbyid(r.id.save);
 deletebtn = (button) findviewbyid(r.id.delete);
 ettext = (edittext) findviewbyid(r.id.ettext);
 c_img = (imageview) findviewbyid(r.id.c_img);
 v_video = (videoview) findviewbyid(r.id.c_video);

 savebtn.setonclicklistener(this);
 deletebtn.setonclicklistener(this);

 notesdb = new notesdb(this);
 dbwriter = notesdb.getwritabledatabase();

 initview();
 }
 //判断存储的是文字,图片,还是视频,启动相对应的控件
 public void initview() {
 if (val.equals("1")) { // 文字
 c_img.setvisibility(view.gone);
 v_video.setvisibility(view.gone);
 }
 if (val.equals("2")) {
 c_img.setvisibility(view.visible);
 v_video.setvisibility(view.gone);

 intent img = new intent(mediastore.action_image_capture);
 phonefile = new file(environment.getexternalstoragedirectory()
  .getabsolutefile() + "/" + gettime() + ".jpg");
 img.putextra(mediastore.extra_output, uri.fromfile(phonefile));
 startactivityforresult(img, 1);
 }
 if (val.equals("3")) {
 c_img.setvisibility(view.gone);
 v_video.setvisibility(view.visible);

 intent video = new intent(mediastore.action_video_capture);
 videofile = new file(environment.getexternalstoragedirectory()
  .getabsolutefile() + "/" + gettime() + ".mp4");
 video.putextra(mediastore.extra_output, uri.fromfile(videofile));
 startactivityforresult(video, 2);
 }
 }

 @override
 public void onclick(view v) {
 switch (v.getid()) {
 case r.id.save:
 adddb();
 finish();
 break;

 case r.id.delete:
 finish();
 break;
 }
 }
 //存储数据
 public void adddb() {
 contentvalues cv = new contentvalues();
 cv.put(notesdb.content, ettext.gettext().tostring());
 cv.put(notesdb.time, gettime());
 cv.put(notesdb.path, phonefile + "");
 cv.put(notesdb.video, videofile + "");
 dbwriter.insert(notesdb.table_name, null, cv);
 }
 //时间的显示
 private string gettime() {
 simpledateformat format = new simpledateformat("yyyy年mm月dd日 hh:mm:ss");
 date curdate = new date();
 string str = format.format(curdate);
 return str;
 }
 //完成数据拍摄后直接显示数据
 @override
 protected void onactivityresult(int requestcode, int resultcode, intent data) {
 super.onactivityresult(requestcode, resultcode, data);

 if (requestcode == 1) {
 bitmap bitmap = bitmapfactory.decodefile(phonefile
  .getabsolutepath());
 c_img.setimagebitmap(bitmap);
 }
 if (requestcode == 2) {
 v_video.setvideouri(uri.fromfile(videofile));
 v_video.start();
 }
 }
}

自定义adapter

public class myadapter extends baseadapter {

 private context context;
 private cursor cursor;
 private linearlayout layout;

 public myadapter(context context, cursor cursor) {
 this.context = context;
 this.cursor = cursor;
 }

 @override
 public int getcount() {
 return cursor.getcount();
 }

 @override
 public object getitem(int position) {
 return cursor.getposition();
 }

 @override
 public long getitemid(int position) {
 // todo auto-generated method stub
 return position;
 }

 public view getview(int position, view convertview, viewgroup parent) {
 layoutinflater inflater = layoutinflater.from(context);
 layout = (linearlayout) inflater.inflate(r.layout.cell, null);

 textview contenttv = (textview) layout.findviewbyid(r.id.list_content);
 textview timetv = (textview) layout.findviewbyid(r.id.list_time);
 imageview imgiv = (imageview) layout.findviewbyid(r.id.list_img);
 imageview videoiv = (imageview) layout.findviewbyid(r.id.list_video);

 cursor.movetoposition(position);

 string content = cursor.getstring(cursor.getcolumnindex("content"));
 string time = cursor.getstring(cursor.getcolumnindex("time"));
 string url = cursor.getstring(cursor.getcolumnindex("path"));
 string urlvideo = cursor.getstring(cursor.getcolumnindex("video"));

 contenttv.settext(content);
 timetv.settext(time);
 imgiv.setimagebitmap(getimagethumbnail(url, 200, 200));
 videoiv.setimagebitmap(getvideothumbnail(urlvideo, 200, 200,
 mediastore.images.thumbnails.micro_kind));
 return layout;
 }
 /** listview 显示图片*/
 public bitmap getimagethumbnail(string uri, int width, int height) {
 bitmap bitmap = null;
 bitmapfactory.options options = new bitmapfactory.options();
 options.injustdecodebounds = true;
 bitmap = bitmapfactory.decodefile(uri, options);
 options.injustdecodebounds = false;
 int bewidth = options.outwidth / width;
 int beheight = options.outheight / height;
 int be = 1;
 if (bewidth < beheight) {
 be = bewidth;
 } else {
 be = beheight;
 }
 if (be <= 0) {
 be = 1;
 }
 options.insamplesize = be;
 bitmap = bitmapfactory.decodefile(uri, options);
 bitmap = thumbnailutils.extractthumbnail(bitmap, width, height,
 thumbnailutils.options_recycle_input);
 return bitmap;
 }
 /**视频缩略图*/
 private bitmap getvideothumbnail(string uri, int width, int height, int kind) {
 bitmap bitmap = null;
 bitmap = thumbnailutils.createvideothumbnail(uri, kind);
 bitmap = thumbnailutils.extractthumbnail(bitmap, width, height,
 thumbnailutils.options_recycle_input);

 return bitmap;
 }

}

listview点击的详情页显示与数据删除

public class selectact extends activity implements onclicklistener {

 private button s_delete, s_back;
 private imageview s_img;
 private videoview s_video;
 private textview s_tv;
 private notesdb notesdb;
 private sqlitedatabase dbwriter;
 /** 点击item 的详情页 对数据进行删除操作*/
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.select);

 s_delete = (button) findviewbyid(r.id.s_delete);
 s_back = (button) findviewbyid(r.id.s_back);
 s_img = (imageview) findviewbyid(r.id.s_img);
 s_video = (videoview) findviewbyid(r.id.s_video);
 s_tv = (textview) findviewbyid(r.id.s_tv);
 /**读写权限*/
 notesdb = new notesdb(this);
 dbwriter = notesdb.getwritabledatabase();

 s_back.setonclicklistener(this);
 s_delete.setonclicklistener(this);
 //判断是否存在图片
 if (getintent().getstringextra(notesdb.path).equals("null")) {
 s_img.setvisibility(view.gone);
 } else {
 s_img.setvisibility(view.visible);
 }

 if (getintent().getstringextra(notesdb.video).equals("null")) {
 s_video.setvisibility(view.gone);
 } else {
 s_video.setvisibility(view.visible);
 }
 // 设置需要显示的图片
 s_tv.settext(getintent().getstringextra(notesdb.content));

 bitmap bitmap = bitmapfactory.decodefile(getintent().getstringextra(
 notesdb.path));
 s_img.setimagebitmap(bitmap);

 s_video.setvideouri(uri
 .parse(getintent().getstringextra(notesdb.video)));

 s_video.start();
 }

 @override
 public void onclick(view v) {
 switch (v.getid()) {
 case r.id.s_delete:
 deletedate();
 finish();
 break;

 case r.id.s_back:
 finish();
 break;
 }
 }
 //数据的删除
 public void deletedate() {
 dbwriter.delete(notesdb.table_name,
 "_id=" + getintent().getintextra(notesdb.id, 0), null);
 }
}

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网