Android Studio 的NotePad制作(日志本)

2023-11-08

自己写的NotePad,一个星期左右的时间。完成了最基本的功能。
但是 界面还是一如既往的shi(因为百度找的图标都不是那种成套的,想找的找不到,干脆下次自己画!)
NotePad的功能无非是对日志的增删改查,这次还加入了Preference的一些配置的设置。这里写图片描述
这是主界面,上方的都是已经建好的日志。
下面的数字和图标对应xml控件的功能为:
①:已经写好的日志的个数
②:系统设置
③:新建日志
④:查找
⑤:锁定界面

先从日志本的数据库讲,基础的数据为日志ID,日志标题,日志内容,日志创建时间。
其次为主题颜色的int值,包括橙色、天蓝、红色等等。
接下来是各种xml布局,基本大同小异,自己想怎么弄就怎么弄。
设置了两个存数据的类,一个是NoteVO为存放ID,Title等关于日志的数据,并写了一个DBAccess里面运用SQL的语法对数据库进行增删改查;还有一个是PrefVO,里面存放了用户的密码以及主题颜色、一些设置保存密码的东西。

为日志本设置了密码,默认密码为空,在设置里面可以进行密码的修改。
设置密码之后,用户可以进行app锁定,下次进来后也要用密码解锁。
主界面为 NotePadMainActivity
除了对图标设置监听事件,比较重要的是 重写 onResum,即刷新界面还有判断此时页面是否锁住,这里写了一个框框加载,代码如下:

 protected void onResume() {
        super.onResume();
       showProgressDialog();

       //判断用户是否上锁,如果上锁,弹出对话框提示用户输入密码
        if(PrefVO.ifLocked){
            final EditText keytext = new EditText(NotePadMainActivity.this);
            keytext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

            AlertDialog.Builder builder = new AlertDialog.Builder(NotePadMainActivity.this);
            builder.setTitle("请输入密码");
            builder.setIcon(R.drawable.suo);
            builder.setView(keytext);
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    if(PrefVO.userPasswordValue.equals(keytext.getText().toString())){
                        PrefVO.appLock(false);
                        Toast.makeText(NotePadMainActivity.this,"已解除锁定",Toast.LENGTH_LONG).show();
                    }
                    else {
                        Toast.makeText(NotePadMainActivity.this,"密码错误",Toast.LENGTH_LONG).show();
                        NotePadMainActivity.this.finish();
                    }
                }
            });
            builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    NotePadMainActivity.this.finish();
                }
            });
            builder.create().show();
        }
    }
 //刷新界面
    private void flush() {
        PrefVO.dataFlush();


        noteVOList = access.findAllNote();
        noteBaseAdapter = new NoteBaseAdapter(NotePadMainActivity.this, R.layout.item, noteVOList);
        noteList.setAdapter(noteBaseAdapter);
        noteNumText.setText(noteVOList.size() + "");
    }





    private class OnItemSelectedListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            note = noteVOList.get(position);

            Intent intent = new Intent();
            intent.setClass(NotePadMainActivity.this, NotePadScanActivity.class);

            Bundle bundle = new Bundle();
            bundle.putParcelable("note", note);
            intent.putExtra("noteBundle", bundle);

            NotePadMainActivity.this.startActivity(intent);
        }
    }


    private void showProgressDialog() {
        pgDialog.setTitle("loading....");
        pgDialog.setMessage("少女祈祷中.....");
        pgDialog.show();
        new progressThread().start();
    }

    private Handler handler = new Handler(){
        public void handleMessage(Message msg){
            flush();
            pgDialog.cancel();
        }
    };

    private class progressThread extends Thread{

        @Override
        public void run() {
            try{
                   Thread.sleep(1000);
                   handler.sendEmptyMessage(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

这里对列表日志设置了一个长按选项,长按后出现删除和短信发送的提示。这里写图片描述
代码如下:

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
        super.onCreateContextMenu(menu,v,menuInfo);

        menu.setHeaderIcon(R.drawable.shezhi);
        menu.setHeaderTitle("日志选项");
        menu.add(0,1,1,"删除");
        menu.add(0,2,2,"短信发送");

    }



    public boolean onContextItemSelected(MenuItem item){
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int index = info.position;
        final NoteVO note = noteVOList.get(index);
        switch (item.getItemId()){
            case 1:{
                AlertDialog.Builder builder = new AlertDialog.Builder(NotePadMainActivity.this);
                builder.setTitle("删除");
                builder.setIcon(R.drawable.shanchu);
                builder.setMessage("你确定要把日志删除吗?");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DBAccess access = new DBAccess(NotePadMainActivity.this);
                        access.deleteNote(note);
                        dialog.dismiss();
                        Toast.makeText(NotePadMainActivity.this,"已删除",Toast.LENGTH_LONG).show();
                        flush();
                    }
                });
                builder.setNegativeButton("取消",null);
                builder.create().show();
                break;
            }
            case 2:{
                Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"));

                if(!note.getNoteContent().equals(note.getNoteTitle())){
                    intent.putExtra("sms_body",note.getNoteContent() + "\n" + note.getNoteTitle());
                }
                else intent.putExtra("sms_body",note.getNoteContent());
                NotePadMainActivity.this.startActivity(intent);

                break;
            }
        }
        return super.onContextItemSelected(item);
    }

此为点击设置之后的界面
这里写图片描述这里写图片描述
设置颜色的代码如下:

 themeList = (ListPreference) findPreference("themelist");
        themeList.setSummary(PrefVO.themeListValue);
        themeList.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                String value = (String)newValue;
                themeList.setSummary(value);
                PrefVO.setThemeListValue(value);

                return true;
            }
        });

设置密码的话,因为我已经设置过了,所以有显示为旧密码,如果第一次设置密码,他只有输入密码和确认密码,两个dialog代码如下:

builder_1 = new AlertDialog.Builder(NotePadPreferenceActivity.this);
        builder_1.setView(linearLayout_1);
        builder_1.setTitle("设置新密码");
        builder_1.setIcon(R.drawable.suo);
        builder_1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String key = newkeyyext.getText().toString();
                String keyagain = newkeyagaintext.getText().toString();

                if(key.equals("") || keyagain.equals("")){
                    Toast.makeText(NotePadPreferenceActivity.this,"密码不能为空",Toast.LENGTH_LONG).show();
                }
                else if(key.equals(keyagain)){
                    PrefVO.setUserPasswordValue(key);
                    usersafety.setTitle("修改密码" );
                }
                else if(!key.equals(keyagain)){
                    Toast.makeText(NotePadPreferenceActivity.this,"两次密码不一致",Toast.LENGTH_LONG).show();
                }
                dialog_1.dismiss();
                clearText();
            }
        });

        builder_1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog_1.dismiss();
                clearText();
            }
        });

        dialog_1 = builder_1.create();

        builder_2 = new AlertDialog.Builder(NotePadPreferenceActivity.this);
        builder_2.setView(linearLayout_2);
        builder_2.setIcon(R.drawable.suo);
        builder_2.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String oldkey = oldkeytext.getText().toString();
                String modifykey = modifykeytext.getText().toString();
                String modifyagainkey = modifykeyagaintext.getText().toString();

                if(!oldkey.equals(PrefVO.userPasswordValue)){
                    Toast.makeText(NotePadPreferenceActivity.this,"密码错误",Toast.LENGTH_LONG).show();
                }
                else if(modifykey.equals("")){
                    Toast.makeText(NotePadPreferenceActivity.this,"密码不能为空",Toast.LENGTH_LONG).show();
                }
                else if(!modifyagainkey.equals(modifyagainkey)){
                    Toast.makeText(NotePadPreferenceActivity.this,"两次输入密码不正确",Toast.LENGTH_LONG).show();
                }
                else if(modifykey.equals(modifyagainkey)){
                    PrefVO.setUserPasswordValue(modifykey);
                }
                dialog_2.dismiss();
                clearText();
            }
        });
        builder_2.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog_2.dismiss();
                clearText();
            }
        });
        dialog_2 = builder_2.create();

        //定义的密码为“”,所以根据密码来判断是第一次设置密码还是修改密码
        usersafety = findPreference("usersafety");
        if(PrefVO.userPasswordValue.equals("")){
            usersafety.setTitle("设置新密码");
        }
        else {
            usersafety.setTitle("修改密码");
        }
        usersafety.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                if(PrefVO.userPasswordValue.equals(""))
                    dialog_1.show();
                else
                    dialog_2.show();
                return false;
            }
        });
    }

    //此方法清空editview,同时要将光标聚焦到第一个EditView
    private void clearText() {
        newkeyyext.setText("");
        newkeyagaintext.setText("");
        newkeyyext.requestFocus();

        oldkeytext.setText("");
        modifykeytext.setText("");
        modifykeyagaintext.setText("");
        oldkeytext.requestFocus();
    }

接下来为新建日志本,这里自己写了两个分别继承TextView和EditView的子控件:NoteTextView,NoteEditView,做了少许的更改。
进入新建页面后,会将焦点放在content上,然后标题跟随打印内容,直到用户自己想写一个标题的时候。在按下返回键后,重写onBackPressed,将新写的日志内容存入到数据库,页面来到查看页面,Toast打印已保存。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-O8cF2dQW-1587976591599)(//img-blog.csdn.net/20180314120118935?watermark/2/text/Ly9ibG9nLmNzZG4ubmV0L3Jpa2thdGhld29ybGQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)]
实现代码如下:

protected void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.edit);
        editLayout = findViewById(R.id.editlayout);
        editLayout.setBackgroundColor(PrefVO.themeColorValue);

        noteTitleText = findViewById(R.id.titleedit);
        noteContentText = findViewById(R.id.contentedit);
        noteContentText.requestFocus();
        noteContentText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if(flagTextChanged && (noteTitleText.getText().toString().trim().equals(null) ||
                noteTitleText.getText().toString().trim().equals(""))){
                    flagTextChanged = true;
                }
                else if(!noteTitleText.getText().toString().equals(noteContentText.getText().toString()))
                    flagTextChanged = false;
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                 if(flagTextChanged)
                     noteTitleText.setText(noteContentText.getText());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void onBackPressed(){
        super.onBackPressed();

        String noteTitle = noteTitleText.getText().toString();
        String noteContent = noteContentText.getText().toString();

        if (noteTitle.toString().trim().equals("") && noteContent.toString().trim().equals(""))
            NotePadNewActivity.this.finish();
        else{
            NoteVO note = new NoteVO();
            note.setNoteTitle(noteTitle);
            note.setNoteContent(noteContent);
            note.setNoteDate(new Date());

            DBAccess access = new DBAccess(this);
            access.insertNote(note);

            Toast.makeText(this,"已保存",Toast.LENGTH_LONG).show();

            Intent intent = new Intent();
            intent.setClass(this,NotePadScanActivity.class);
            Bundle bundle = new Bundle();
            bundle.putParcelable("note",note);
            intent.putExtra("noteBundle",bundle);

            this.startActivity(intent);
            this.finish();
        }
    }

进入查看界面有三个可选操作:编辑,删除,短信发送。编辑即返回到edit的页面。
这里写图片描述这里写图片描述这里写图片描述

实现代码如下:

scanLayout = findViewById(R.id.scanlayout);
        scanLayout.setBackgroundColor(PrefVO.themeColorValue);

        noteTitleText = findViewById(R.id.titlescan);
        noteContentText = findViewById(R.id.contentscan);
        noteContentText.setMovementMethod(ScrollingMovementMethod.getInstance());
        notedateText = findViewById(R.id.datescan);

        intent = this.getIntent();
        Bundle bundle = intent.getBundleExtra("noteBundle");
        noteVO = bundle.getParcelable("note");

        noteTitleText.setText(noteVO.getNoteTitle());
        notedateText.setText(ConvertDate.datetoString(noteVO.getNoteDate()));
        noteContentText.setText(noteVO.getNoteContent());

        bianji = findViewById(R.id.bianji);
        bianji.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Intent intent = new Intent();
                intent.setClass(NotePadScanActivity.this,NotePadEditActivity.class);

                Bundle bundle= new Bundle();
                bundle.putParcelable("note",noteVO);
                intent.putExtra("noteBundle",bundle);

                NotePadScanActivity.this.startActivity(intent);
                NotePadScanActivity.this.finish();
                return false;
            }
        });

        duanxin = findViewById(R.id.duanxin);
        duanxin.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"));

                if(!noteVO.getNoteContent().equals(noteVO.getNoteTitle())){
                    intent.putExtra("sms_body", noteVO.getNoteTitle() + "\n" + noteVO.getNoteContent());
                }
                else {
                    intent.putExtra("sms_body", noteVO.getNoteContent());
                }
                NotePadScanActivity.this.startActivity(intent);

                return false;
            }
        });

        shanchu = findViewById(R.id.shanchu);
        shanchu.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                AlertDialog.Builder builder = new AlertDialog.Builder(NotePadScanActivity.this);
                builder.setTitle("删除");
                builder.setIcon(R.drawable.shanchu);
                builder.setMessage("你确定要把日志删除吗?");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DBAccess access = new DBAccess(NotePadScanActivity.this);
                        access.deleteNote(noteVO);

                        dialog.dismiss();
                        Toast.makeText(NotePadScanActivity.this, "已删除", Toast.LENGTH_LONG).show();
                        NotePadScanActivity.this.finish();
                    }
                });
                builder.setNegativeButton("取消", null);
                builder.create().show();

                return false;
            }
        });

最后则是查找操作
这里写图片描述
在mainfest设置为对话框形式,写了一个NoteCursorAdapter继承CursorAdapter,对列表日志的标题和内容进行查找,用到AutoCompleteTextView代码如下:

 protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.search);

        access = new DBAccess(this);

        c = access.selectAllNoteCursor(null,null);
        noteCursorAdapter = new NoteCursorAdapter(this,c,true);
        searchTextView = findViewById(R.id.searchtext);
        searchTextView.setDropDownHeight(0);
        searchTextView.requestFocus();
        searchTextView.setAdapter(noteCursorAdapter);

        searchListView = findViewById(R.id.searchList);
        searchListView.setAdapter(noteCursorAdapter);
        searchListView.setOnItemClickListener(new OnItemSelectListener());
    }

    private class OnItemSelectListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           note = noteCursorAdapter.getList().get(position);

           Intent intent = new Intent();
           intent.setClass(NotePadSearchActivity.this,NotePadScanActivity.class);

           Bundle bundle = new Bundle();
           bundle.putParcelable("note",note);
           intent.putExtra("noteBundle",bundle);

           NotePadSearchActivity.this.startActivity(intent);
        }
    }

至此为止,功能基本实现,其他的代码就不放了,等下挂GIT
emmm由于在debug的时候有很多数据库的单词填错或者没写空格,几乎浪费了一个上午的时间,还有参考的书籍实在太过老旧,menuItem在AS3.0已经不用,在那上面还是浪费了很多时间。
这次的话我注意了代码的美观,尽量让代码看起来易懂一点,虽然注释也少了一点…

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android Studio 的NotePad制作(日志本) 的相关文章

随机推荐

  • [STM32WBA]【STM32WBA52CG测评】-3- 蓝牙BLE:LED与button例程分析

    STM32WBA52CG是支持蓝牙BLE 5 3 官方提供的STM32Cube FW WBA V1 1 0资料包中提供了一个非常好的入门案例 BLE p2pServer 准备材料 Keil ST BLE Toolbox 图形化配置时钟 主要
  • QT安装mqtt环境(这里默认以及有了QT)

    首先 QT的版本和mqtt包的版本要一致 我这里QT和mqtt的版本都是5 14 2 QT安装包 5 14 2 下载地址 Index of archive qt 5 14 mqtt包的一个连接 可以选择相应的版本 GitHub qt qtm
  • eclipse easyui 正常代码老是报错 红色波浪线

    即使交换位置 手敲行依旧报错 看了三篇 还是看不出问题 关于正确代码会出现很多红色波浪线 网上的办法是把eclipse软件关闭 然后重新启动即可消除 但是这种方法有个弊端 当再次编辑的时候依旧很出现很多波浪线 尝试了以下两种方法 https
  • Retrofit 2.5 框架使用与源码分析

    Retrofit 2 5 框架使用与源码分析 Retrofit 框架使用 请求内容与返回值 使用PostMan进行请求测试 请求 https api github com search repositories q android 返回值
  • 【计算机视觉

    文章目录 一 检测相关 7篇 1 1 Vehicle Occurrence based Parking Space Detection 1 2 Squeezing nnU Nets with Knowledge Distillation f
  • LeetCode--Intersection of Two Linked Lists (两个链表的交点)Python

    题目 给定两个链表 求这两个链表的交点 若没有交点 则返回空 样例如下 返回交点c1 解题思路 思路1 暴力思路 n方复杂度 对两个链表分别进行遍历 找到相同的节点即可O n m 空间复杂度为O 1 思路2 使用哈希表 即python中的字
  • 6-6 找素数并保存到数组中

    本题目要求查找n m之间所有素数 存入一维数组a中 函数接口定义 int fun int n int m int a 其中 a 为存储的素数 函数返回素数的个数 裁判测试程序样例 include
  • 编译工具链和交叉编译工具链简易说明

    文章目录 编译工具链 交叉编译工具链 编译工具链 做C C 开发特别是嵌入式方向的肯定会涉及编译工具链和交叉编译工具链相关内容 C C 的程序需要经过 gcc 等编译成二进制程序才能被计算机使用 这里的 gcc 通常是泛指 包括 gcc g
  • 关于stm32下载提示internal command error错误提示解决办法

    问题背景 最近新制作了一个关于stm32的PCB PCB电源供电是由12V降压5V 再降压到3 3V 并且预留了3 3V电源接口 打样贴片完成后准备下载程序 一开始我是为了测试方便 没有用12V供电 直接连接stlink 并且用了电脑5V降
  • 高效大数据开发之 bitmap 思想的应用

    作者 xmxiong PCG 运营开发工程师 数据仓库的数据统计 可以归纳为三类 增量类 累计类 留存类 而累计类又分为历史至今的累计与最近一段时间内的累计 比如滚动月活跃天 滚动周活跃天 最近 N 天消费情况等 借助 bitmap 思想统
  • mysql主从配置问题汇总及如何查看数据库的日志

    一 Could not execute Delete rows event on table yxjmanage ums user Can t find record in ums user Error code 1032 handler
  • 原生ajax运行原理,【前端自学之路】JS之原生AJAX原理

    Javascript Ajax 原理 什么是 AJAX AJAX Asynchronous JavaScript and XML 异步Javascript 和 XML AJAX 是指一种创建动态网页的开发技术 说白话点 AJAX 就是允许J
  • Android常见问题debug

    android util Log 常用的方法有以下5个 Log v Log d Log i Log w Log e 根据首字母对应 VERBOSE DEBUG INFO WARN ERROR 另外 Log太多时用来过滤和标识分类log信息
  • Git简单入门(学习笔记)

    Git简单入门 学习笔记 目录 一 Git概念 二 版本控制 三 Git下载与安装 四 Git结构 五 本地库与远程库的交互方式 六 代码托管中心 七 初始化本地仓库 一 Git概念 Git是一个免费的开源的分布式版本控制系统 可以快速高效
  • python在线评测系统_关于开源OJ_在线评测系统(Online Judge)设计与实现的研究与分析...

    标签 OJ是Online Judge系统的简称 用来在线检测程序源代码的正确性 著名的OJ有TYVJ RQNOJ URAL等 国内著名的题库有北京大学题库 浙江大学题库 电子科技大学题库 杭州电子科技大学等 国外的题库包括乌拉尔大学 瓦拉杜
  • LVS 之 集群搭建

    官网地址 http www linuxvirtualserver org zh lvs1 html 首先 准备4台虚拟机 一个用于客户端 一个用于LVS 调度器 2个用于后端服务器 LVS NAT配置 1 zk02 开启内核的核心转发功能
  • 在Python中使用StanfordOpenIE

    本文在 维基百科数据预处理的基础上进行 1 StanfordOpenIE简介 开放信息提取 open IE 是指从纯文本中提取关系元组 通常是二元关系 例如 Mark Zuckerberg 脸书 与其他信息提取的核心区别在于 这些关系的模式
  • mysql配置和使用中可能会出现的若干问题

    Manually delete the data folder created by yourself 删除自行创建的data文件夹 Then enter the bin directory under the administrator
  • QT应用开发基础

    目录 前言 Windows上搭建开发环境 C 基础 什么是C 什么是面向对象 什么又是面向过程 c 的灵魂 c 的类 对象 类的实例化 怎么访问类的成员 类的函数成员 类的访问修饰符 函数的重载 构造函数和析构函数 类的继承 虚函数和纯虚函
  • Android Studio 的NotePad制作(日志本)

    自己写的NotePad 一个星期左右的时间 完成了最基本的功能 但是 界面还是一如既往的shi 因为百度找的图标都不是那种成套的 想找的找不到 干脆下次自己画 NotePad的功能无非是对日志的增删改查 这次还加入了Preference的一