【NotesPad功能】

本文详细介绍了Android笔记应用NotesPad的主要功能,包括自定义EditText视图在行间绘制线条、置顶功能、剪贴板导入笔记、删除笔记的实现代码,以及各功能的运行效果展示。通过这些功能,NotesPad提供了一个强大且用户友好的笔记管理体验。
摘要由CSDN通过智能技术生成

前言

介绍部分NotesPad的基本功能和额外添加的功能

一、NotesPad功能

Notespad是基于Android studio的一种功能,该工具是为了解决记录事件而创建的

代码

##1.NoteEditor的拓展活动:
代码如下:

public class NoteEditor extends Activity {
    // For logging and debugging purposes
    private static final String TAG = "NoteEditor"
    /*
     * Creates a projection that returns the note ID and the note contents.
     */
    private static final String[] PROJECTION =
        new String[] {
            NotePad.Notes._ID,
            NotePad.Notes.COLUMN_NAME_TITLE,
            NotePad.Notes.COLUMN_NAME_NOTE
    };

2.部分功能介绍

(1)Creates a projection that returns the note ID and the note contents:

代码如下:

public static class LinedEditText extends EditText {
        private Rect mRect;
        private Paint mPaint;
        // This constructor is used by LayoutInflater
        public LinedEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
            // Creates a Rect and a Paint object, and sets the style and color of the Paint object.
            mRect = new Rect();
            mPaint = new Paint();
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(0x800000FF);
        }

(2)定义自定义EditText视图,该视图在显示的每一行文本之间绘制线条:

代码如下:

public static class LinedEditText extends EditText {
        private Rect mRect;
        private Paint mPaint;
 
        // This constructor is used by LayoutInflater
        public LinedEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
 
            // Creates a Rect and a Paint object, and sets the style and color of the Paint object.
            mRect = new Rect();
            mPaint = new Paint();
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(0x800000FF);
        }

(3)置顶功能:

在Activity即将到达前台时被调用。这发生在Activity到达任务栈的顶部时,或者当它第一次启动时。移动到列表中的第一个注释,为用户选择的操作设置适当的标题,将注释内容放TextView1中,并保存原始文本作为备份。
代码如下:

mCursor.moveToFirst();
            // Modifies the window title for the Activity according to the current Activity state.
            if (mState == STATE_EDIT) {
                // Set the title of the Activity to include the note title
                int colTitleIndex = mCursor.getColumnIndex(NotePad.Notes.COLUMN_NAME_TITLE);
                String title = mCursor.getString(colTitleIndex);
                Resources res = getResources();
                String text = String.format(res.getString(R.string.title_edit), title);
                setTitle(text);
            // Sets the title to "create" for inserts
            } else if (mState == STATE_INSERT) {
                setTitle(getText(R.string.title_create));
            }
 
            
            int colNoteIndex = mCursor.getColumnIndex(NotePad.Notes.COLUMN_NAME_NOTE);
            String note = mCursor.getString(colNoteIndex);
            mText.setTextKeepState(note);
 
            // Stores the original note text, to allow the user to revert changes.
            if (mOriginalContent == null) {
                mOriginalContent = note;
            }
 
        /*
         * Something is wrong. The Cursor should always contain data. Report an error in the
         * note.
         */
        } else {
            setTitle(getText(R.string.error_title));
            mText.setText(getText(R.string.error_message));
        }
    }

运行效果:
在这里插入图片描述

(4)剪贴板导入NotesPad

一种用剪贴板的内容替换笔记数据的辅助方法。
代码如下:

private final void performPaste() {
 
        // Gets a handle to the Clipboard Manager
        ClipboardManager clipboard = (ClipboardManager)
                getSystemService(Context.CLIPBOARD_SERVICE);
 
        // Gets a content resolver instance
        ContentResolver cr = getContentResolver();
 
        // Gets the clipboard data from the clipboard
        ClipData clip = clipboard.getPrimaryClip();
        if (clip != null) {
 
            String text=null;
            String title=null;
 
            // Gets the first item from the clipboard data
            ClipData.Item item = clip.getItemAt(0);
 
            // Tries to get the item's contents as a URI pointing to a note
            Uri uri = item.getUri();
 
            if (uri != null && NotePad.Notes.CONTENT_ITEM_TYPE.equals(cr.getType(uri))) {
 
                Cursor orig = cr.query(
                        uri,            // URI for the content provider
                        PROJECTION,     // Get the columns referred to in the projection
                        null,           // No selection variables
                        null,           // No selection variables, so no criteria are needed
                        null            // Use the default sort order
                );
 
                // If the Cursor is not null, and it contains at least one record
                // (moveToFirst() returns true), then this gets the note data from it.
                if (orig != null) {
                    if (orig.moveToFirst()) {
                        int colNoteIndex = mCursor.getColumnIndex(NotePad.Notes.COLUMN_NAME_NOTE);
                        int colTitleIndex = mCursor.getColumnIndex(NotePad.Notes.COLUMN_NAME_TITLE);
                        text = orig.getString(colNoteIndex);
                        title = orig.getString(colTitleIndex);
                    }
 
                    // Closes the cursor.
                    orig.close();
                }
            }
 
            // If the contents of the clipboard wasn't a reference to a note, then
            // this converts whatever it is to text.
            if (text == null) {
                text = item.coerceToText(this).toString();
            }
 
            // Updates the current note with the retrieved title and text.
            updateNote(text, title);
        }
    }

运行效果图:
在这里插入图片描述

(5)删除记事本

Take care of deleting a note. Simply deletes the entry

代码如下:

   private final void deleteNote() {
        if (mCursor != null) {
            mCursor.close();
            mCursor = null;
            getContentResolver().delete(mUri, null, null);
            mText.setText("");
        }
    }

运行效果图:
在这里插入图片描述

总结

以上就是这次博客中讲到的内容,篇幅有限,不能面面俱到。

作者:闪电侠骑自行车

原文地址: https://blog.csdn.net/m0_46458494/article/details/121882624.


  1. TextView是NotesPad项目主页展示。 ↩︎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闪电侠骑自行车

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值