大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Android实现记事本功能

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

大祥网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。创新互联从2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联

实现功能

1、文本数据的存储

2、图片数据存储

3、视频数据存储

4、自定义的Adapter

5、SQlite的创建

6、数据listview列表的显示

demo地址

记事本

Android实现记事本功能

界面布局

Android实现记事本功能



 

 

数据库创建

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实现记事本功能
分享地址:http://dzwzjz.com/article/jicehg.html
在线咨询
服务热线
服务热线:028-86922220
TOP