Android开发---记事本(二)
来源:互联网 发布:mac使用github pages 编辑:程序博客网 时间:2024/06/08 10:54
这次完善了上次的记事本小程序,脱了很久了,主要是工作太烦心。
上一篇博客:http://blog.csdn.net/u013318615/article/details/47220767
完善内容:
1.修改了一些图标,美化UI。
2.主界面笔记列表添加侧滑删除。
3.添加修改笔记功能。
侧滑菜单
侧滑删除主要借鉴了IOS的侧滑删除,用到了Swipemenulistview这个类库。
首先在布局文件中添加Swipemenulistview
<com.baoyz.swipemenulistview.SwipeMenuListView android:id="@+id/listview_main" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/layout_bottom" android:layout_alignParentTop="true" android:listSelector="@color/transparent" android:scrollbars="none" />
然后初始化菜单选项:
// 侧滑删除的creator creator = new SwipeMenuCreator() { @Override public void create(SwipeMenu menu) { // 创建删除项 SwipeMenuItem deleteItem = new SwipeMenuItem( getApplicationContext()); // 设置背景 deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25))); // 设置宽度 deleteItem.setWidth(dp2px(60)); // 设置图标 deleteItem.setIcon(R.mipmap.ic_delete); // 添加到menu列表 menu.addMenuItem(deleteItem); } private int dp2px(int dp) { return (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, dp, getResources() .getDisplayMetrics()); } };
当然侧滑菜单不仅可以删除,还可以通过menu.addMenuItem方法添加别的功能,比如点赞或者分享等等。
listView.setMenuCreator(NotePadApp.creator);
将菜单选项加入到listview中。
添加侧滑监听:
listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(int position, SwipeMenu swipeMenu, int index) { // index:菜单中第几项 if(index == 0) { Note note = notes.get(position); DbUtils dbUtils = DbUtils.create(MainActivity.this); try { dbUtils.delete(note); dbUtils.findAll(Note.class); notes.remove(position); mAdapter.notifyDataSetChanged(); } catch (DbException e) { e.printStackTrace(); Toast.makeText(MainActivity.this,"删除失败",Toast.LENGTH_SHORT).show(); } } // false:关闭菜单 true:不关闭菜单 return true; } });
监听方法中的index就是侧滑菜单中的选项的索引,因为我只添加了删除,所以index==0。
修改笔记
@OnItemClick(R.id.listview_main) public void onItemClickListener(AdapterView<?> parent,View view, int position,long id) { Note note = notes.get(position); Intent intent = new Intent(MainActivity.this,AddNoteActivity.class); intent.putExtra("note",note); startActivity(intent); }
点击listview的时候,将note实例传递到编辑界面。
private void init() { mNote = (Note) getIntent().getSerializableExtra("note"); if(mNote != null) { edt_content.setText(mNote.getContent()); } }
接收实例并展示,然后用户就可以做修改了。
Note note = null; if(mNote == null) { note = new Note(edt_content.getText().toString(), new Date(System.currentTimeMillis())); }else { mNote.setContent(edt_content.getText().toString()); mNote.setDate(new Date(System.currentTimeMillis())); note = mNote; } DbUtils dbUtils = DbUtils.create(this); try { if(note != null) { dbUtils.saveOrUpdate(note); //ArrayList<Note> n = (ArrayList<Note>) dbUtils.findAll(Note.class); } return true; } catch (DbException e) { e.printStackTrace(); Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show(); return false; }
保存的时候,通过传递过来的mNote来判断是否是修改笔记。
效果图
最后就是展示效果了:
这次的记事本就开发完成了,可以装到手机上使用了。
1 0
- Android开发---记事本(二)
- android开发---记事本(一)
- Android开发之----记事本程序实作训练(二)
- android 简单记事本开发(一)
- Android记事本开发
- Android记事本的开发
- android记事本开发后记
- Android记事本开发之界面设计
- Sencha Touch开发实例:记事本应用(二)
- Java开发记事本(完整版)
- IOS 开发记事本(kf_setImage)
- Android记事本开发之SQLite数据库实现
- 代码搭建记事本框架(二)
- android 记事本
- Android开发(二)
- Android开发【记事本App】项目开发流程——1
- Android开发—数据库应用—添加列表活动(ListActivity)--分析记事本程序
- 简易记事本开发(第二次C#作业)
- mysql_thread_init()
- mysql数据库常用的基本SQL语句--数据库表操作
- Qt Scene Graph渲染方式在不同系统的差异
- Eclipse中新建工程出现红色感叹号以及appcompat-v7编译错误
- lintcode-插入区间
- Android开发---记事本(二)
- fragment+RadioGroup实现底部导航栏 多个 fragment 重影问题
- Leet Code 4 Median of Two Sorted Arrays
- Divisibility by Eight
- SpringMVC的@ModelAttribute和@RequestParam、@PathVariable、@CookieValue、@SessionAttributes
- 【OpenCV 基础】Mat和IplImage间的相互转换
- java实现最基础的中国5角星
- Gradle文件结构介绍
- 【CCF】命令行选项