Android实现记事本功能

Android实现记事本功能,第1张

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

本文实例为大家分享了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,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,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 height,int kind) { Bitmap bitmap = null; bitmap = thumbnailUtils.createVIDeothumbnail(uri,kind); bitmap = thumbnailUtils.extractthumbnail(bitmap,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); }}

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

总结

以上是内存溢出为你收集整理的Android实现记事本功能全部内容,希望文章能够帮你解决Android实现记事本功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1143526.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存