📄 noteeditor.java
字号:
package com.android.notepad;import com.android.notepad.NotePad.Notes;import android.app.Activity;import android.content.ComponentName;import android.content.ContentValues;import android.content.Context;import android.content.Intent;import android.database.Cursor;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Rect;import android.net.Uri;import android.os.Bundle;import android.util.AttributeSet;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.widget.EditText;public class NoteEditor extends Activity { private static final String TAG = "Notes"; private int state_var_A; private static final String[] PROJECTION = new String[] { Notes._ID, // 0 Notes.NOTE, // 1 }; private static final int COLUMN_INDEX_NOTE = 1; private static final String ORIGINAL_CONTENT = "origContent"; private static final int REVERT_ID = Menu.FIRST; private static final int DISCARD_ID = Menu.FIRST + 1; private static final int DELETE_ID = Menu.FIRST + 2; private boolean mNoteOnly = false; private Uri mUri; private Cursor mCursor; private EditText mText; private String mOriginalContent; public static class LinedEditText extends EditText { private Rect mRect; private Paint mPaint; public LinedEditText(Context context, AttributeSet attrs) { super(context, attrs); mRect = new Rect(); mPaint = new Paint(); mPaint.setStyle(Paint.Style.STROKE); mPaint.setColor(0x800000FF); } @Override protected void onDraw(Canvas canvas) { int count = getLineCount(); Rect r = mRect; Paint paint = mPaint; for (int i = 0; i < count; i++) { int baseline = getLineBounds(i, r); canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint); } super.onDraw(canvas); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); goto_state_1(savedInstanceState); } void goto_state_1(Bundle savedInstanceState) { state_var_A = 1; final Intent intent = getIntent(); final String action = intent.getAction(); if (Intent.ACTION_INSERT.equals(action)) { goto_state_2(savedInstanceState, intent); } else if (Intent.ACTION_EDIT.equals(action)) { goto_state_3(savedInstanceState, intent); } else { Log.e(TAG, "Unknown action, exiting"); goto_state_4(); } } void goto_state_2(Bundle savedInstanceState, Intent intent) { state_var_A = 2; mUri = getContentResolver().insert(intent.getData(), null); if (mUri == null) { Log.e(TAG, "Failed to insert new note into " + getIntent().getData()); goto_state_4(); } else { setResult(RESULT_OK, (new Intent()).setAction(mUri.toString())); setContentView(R.layout.note_editor); mText = (EditText) findViewById(R.id.note); mCursor = managedQuery(mUri, PROJECTION, null, null, null); if (savedInstanceState != null) { mOriginalContent = savedInstanceState.getString(ORIGINAL_CONTENT); } } } void goto_state_3(Bundle savedInstanceState, Intent intent) { state_var_A = 3; mUri = intent.getData(); setContentView(R.layout.note_editor); mText = (EditText) findViewById(R.id.note); mCursor = managedQuery(mUri, PROJECTION, null, null, null); if (savedInstanceState != null) { mOriginalContent = savedInstanceState.getString(ORIGINAL_CONTENT); } } void goto_state_4() { state_var_A = 4; finish(); return; } @Override protected void onResume() { super.onResume(); if (mCursor != null) { mCursor.moveToFirst(); if (state_var_A == 2) { setTitle(getText(R.string.title_edit)); } else if (state_var_A == 3) { setTitle(getText(R.string.title_create)); } String note = mCursor.getString(COLUMN_INDEX_NOTE); mText.setTextKeepState(note); if (mOriginalContent == null) { mOriginalContent = note; } } else { setTitle(getText(R.string.error_title)); mText.setText(getText(R.string.error_message)); } } @Override protected void onSaveInstanceState(Bundle outState) { outState.putString(ORIGINAL_CONTENT, mOriginalContent); } @Override protected void onPause() { super.onPause(); if (mCursor != null) { String text = mText.getText().toString(); int length = text.length(); if (isFinishing() && (length == 0) && !mNoteOnly) { setResult(RESULT_CANCELED); deleteNote(); // Get out updates into the provider. } else { ContentValues values = new ContentValues(); // This stuff is only done when working with a full-fledged note. if (!mNoteOnly) { // Bump the modification time to now. values.put(Notes.MODIFIED_DATE, System.currentTimeMillis()); // If we are creating a new note, then we want to also create // an initial title for it. if (state_var_A == 2) { String title = text.substring(0, Math.min(30, length)); if (length > 30) { int lastSpace = title.lastIndexOf(' '); if (lastSpace > 0) { title = title.substring(0, lastSpace); } } values.put(Notes.TITLE, title); } } // Write our text back into the provider. values.put(Notes.NOTE, text); // Commit all of our changes to persistent storage. When the update completes // the content provider will notify the cursor of the change, which will // cause the UI to be updated. getContentResolver().update(mUri, values, null, null); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); if (state_var_A == 3) { menu.add(0, REVERT_ID, 0, R.string.menu_revert) .setShortcut('0', 'r') .setIcon(android.R.drawable.ic_menu_revert); if (!mNoteOnly) { menu.add(0, DELETE_ID, 0, R.string.menu_delete) .setShortcut('1', 'd') .setIcon(android.R.drawable.ic_menu_delete); } } else { menu.add(0, DISCARD_ID, 0, R.string.menu_discard) .setShortcut('0', 'd') .setIcon(android.R.drawable.ic_menu_delete); } if (!mNoteOnly) { Intent intent = new Intent(null, getIntent().getData()); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, new ComponentName(this, NoteEditor.class), null, intent, 0, null); } return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle all of the possible menu actions. switch (item.getItemId()) { case DELETE_ID: deleteNote(); goto_state_4(); break; case DISCARD_ID: cancelNote(); break; case REVERT_ID: cancelNote(); break; } return super.onOptionsItemSelected(item); } /** * Take care of canceling work on a note. Deletes the note if we * had created it, otherwise reverts to the original text. */ private final void cancelNote() { if (mCursor != null) { if (state_var_A == 3) { // Put the original note text back into the database mCursor.close(); mCursor = null; ContentValues values = new ContentValues(); values.put(Notes.NOTE, mOriginalContent); getContentResolver().update(mUri, values, null, null); } else if (state_var_A == 2) { // We inserted an empty note, make sure to delete it deleteNote(); } } setResult(RESULT_CANCELED); finish(); } /** * 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(""); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -