Android开发---记事本(二)

这次完善了上次的记事本小程序,脱了很久了,主要是工作太烦心。
上一篇博客: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来判断是否是修改笔记。

效果图

最后就是展示效果了:
这里写图片描述
这次的记事本就开发完成了,可以装到手机上使用了。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值