Android实现记事本功能
时间:2020-10-26 23:02:16|栏目: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, 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); } }
上一篇:关于Android Studio封装SDK的那些事儿
栏 目:Android代码
下一篇:Android编程实现获取多媒体库视频、音频、图片的方法
本文标题:Android实现记事本功能
本文地址:http://www.codeinn.net/misctech/16002.html