您的位置:首页 > 编程语言 > Java开发

三、NoteEditor.java文件学习笔记

2015-09-03 20:26 537 查看
这部分代码控制编辑Note的活动。

首先看一下布局文件,<view xmlns:android="http://schemas.android.com/apk/res/android"
class="com.example.android.notepad.NoteEditor$LinedEditText"
android:id="@+id/note"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:padding="5dp"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:gravity="top"
android:textSize="22sp"
android:capitalize="sentences"
/>一个自定义的布局文件,class=这一句指明了哪个类控制这个布局文件。
再来看代码的结构:



LineEditText这个类继承了EditText,实现了onDraw方法。

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);
}
}
这儿理解还是不够,以后补充
回过头来看AndroidManifest.xml文件中NoteEditor Activity的配置。

<activity android:name="NoteEditor"
android:theme="@android:style/Theme.Holo.Light"
android:screenOrientation="sensor"

4000
android:configChanges="keyboardHidden|orientation"
>
<!-- This filter says that we can view or edit the data of
a single note -->
<intent-filter android:label="@string/resolve_edit">
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<action android:name="com.android.notepad.action.EDIT_NOTE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
</intent-filter>

<!-- This filter says that we can create a new note inside
of a directory of notes. The INSERT action creates an
empty note; the PASTE action initializes a new note from
the current contents of the clipboard. -->
<intent-filter>
<action android:name="android.intent.action.INSERT" />
<action android:name="android.intent.action.PASTE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
</intent-filter>

</activity>
NoteEditor的action接受的有EDIT,VIEW,EDIT_NOTE,INSERT,PASTE,DEFSULT。
接着看NoteEditor.java。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

final Intent intent = getIntent();
final String action = intent.getAction();
if (Intent.ACTION_EDIT.equals(action)) {
mState = STATE_EDIT;
mUri = intent.getData();
} else if (Intent.ACTION_INSERT.equals(action)
mState = STATE_INSERT;
mUri = getContentResolver().insert(intent.getData(), null);
if (mUri == null) {

Log.e(TAG, "Failed to insert new note into " + getIntent().getData());

finish();
return;
}
setResult(RESULT_OK, (new Intent()).setAction(mUri.toString()));
} else {

Log.e(TAG, "Unknown action, exiting");
finish();
return;
}

mCursor = managedQuery(
mUri, // The URI that gets multiple notes from the provider.
PROJECTION, // A projection that returns the note ID and note content for each note.
null, // No "where" clause selection criteria.
null, // No "where" clause selection values.
null // Use the default sort order (modification date, descending)
);
if (Intent.ACTION_PASTE.equals(action)) {
// Does the paste
performPaste();
// Switches the state to EDIT so the title can be modified.
mState = STATE_EDIT;
}

setContentView(R.layout.note_editor);

mText = (EditText) findViewById(R.id.note);

if (savedInstanceState != null) {
mOriginalContent = savedInstanceState.getString(ORIGINAL_CONTENT);
}
}


做的无非是根据action参数得到mState,表明要干什么,intent参数获得mUri,查询得到mCursor。
protected void onResume() {
super.onResume();

if (mCursor != null) {
// Requery in case something changed while paused (such as the title)
mCursor.requery();

mCursor.moveToFirst();

if (mState == STATE_EDIT) {
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);
} 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);
if (mOriginalContent == null) {
mOriginalContent = note;
}

} else {
setTitle(getText(R.string.error_title));
mText.setText(getText(R.string.error_message));
}
}
设置title及edittext中的文本。
protected void onSaveInstanceState(Bundle outState) {
// Save away the original text, so we still have it if the activity
// needs to be killed while paused.
outState.putString(ORIGINAL_CONTENT, mOriginalContent);
}
在活动结束前保存最初文本。
protected void onPause() {
super.onPause();

if (mCursor != null) {

String text = mText.getText().toString();
int length = text.length();
if (isFinishing() && (length == 0)) {
setResult(RESULT_CANCELED);
deleteNote();

} else if (mState == STATE_EDIT) {
updateNote(text, null);
} else if (mState == STATE_INSERT) {
updateNote(text, text);
mState = STATE_EDIT;
}
}
}


onPause函数,一旦活动不再前台,如果取消,就删除修改内容,编辑,就更新内容,插入,的话也是一样
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu from XML resource
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.editor_options_menu, menu);

// Only add extra menu items for a saved note
if (mState == STATE_EDIT) {

Intent intent = new Intent(null, mUri);
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0,
new ComponentName(this, NoteEditor.class), null, intent, 0, null);
}

return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// Check if note has changed and enable/disable the revert option
int colNoteIndex = mCursor.getColumnIndex(NotePad.Notes.COLUMN_NAME_NOTE);
String savedNote = mCursor.getString(colNoteIndex);
String currentNote = mText.getText().toString();
if (savedNote.equals(currentNote)) {
menu.findItem(R.id.menu_revert).setVisible(false);
} else {
menu.findItem(R.id.menu_revert).setVisible(true);
}
return super.onPrepareOptionsMenu(menu);
}


上面的代码是右键菜单的代码,来看第一个函数是显示editor_options_menue中的内容为菜单内容,第二个根据内容是否改变决定revert菜单项是否显示。
public boolean onOptionsItemSelected(MenuItem item) {
// Handle all of the possible menu actions.
switch (item.getItemId()) {
case R.id.menu_save:
String text = mText.getText().toString();
updateNote(text, null);
finish();
break;
case R.id.menu_delete:
deleteNote();
finish();
break;
case R.id.menu_revert:
cancelNote();
break;
}
return super.onOptionsItemSelected(item);
}菜单项的交互。
private final void performPaste() {

ClipboardManager clipboard = (ClipboardManager)
getSystemService(Context.CLIPBOARD_SERVICE);
ContentResolver cr = getContentResolver();

ClipData clip = clipboard.getPrimaryClip();
if (clip != null) {

String text=null;
String title=null;

ClipData.Item item = clip.getItemAt(0);

Uri uri = item.getUri();

if (uri != null && NotePad.Notes.CONTENT_ITEM_TYPE.equals(cr.getType(uri))) {

// The clipboard holds a reference to data with a note MIME type. This copies it.
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);
}
}


这个是剪切操作的实现,如果剪切的是Note,就查询,获取text,tilte,如果文本,直接作为text,然后调用updateNote()函数保存更改。
private final void updateNote(String text, String title) {

// Sets up a map to contain values to be updated in the provider.
ContentValues values = new ContentValues();
values.put(NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE, System.currentTimeMillis());

// If the action is to insert a new note, this creates an initial title for it.
if (mState == STATE_INSERT) {

// If no title was provided as an argument, create one from the note text.
if (title == null) {

// Get the note's length
int length = text.length();

title = text.substring(0, Math.min(30, length));
if (length > 30) {
int lastSpace = title.lastIndexOf(' ');
if (lastSpace > 0) {
title = title.substring(0, lastSpace);
}
}
}
// In the values map, sets the value of the title
values.put(NotePad.Notes.COLUMN_NAME_TITLE, title);
} else if (title != null) {
// In the values map, sets the value of the title
values.put(NotePad.Notes.COLUMN_NAME_TITLE, title);
}

// This puts the desired notes text into the map.
values.put(NotePad.Notes.COLUMN_NAME_NOTE, text);
getContentResolver().update(
mUri, // The URI for the record to update.
values, // The map of column names and new values to apply to them.
null, // No selection criteria are used, so no where columns are necessary.
null // No where columns are used, so no where arguments are necessary.
);
}


updateNote这个函数,如果title存在,将text,title存入,否则取前30字符为title。

private final void cancelNote() {
if (mCursor != null) {
if (mState == STATE_EDIT) {
// Put the original note text back into the database
mCursor.close();
mCursor = null;
ContentValues values = new ContentValues();
values.put(NotePad.Notes.COLUMN_NAME_NOTE, mOriginalContent);
getContentResolver().update(mUri, values, null, null);
} else if (mState == STATE_INSERT) {
// 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("");
}
}
}cancleNote如果取消,就将mOrigContent存回去。
deleteNote删除一条note,将EditText设为空白行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: