[Android learning 13th] -- SDK instance notepad Analysis 2

Source: Internet
Author: User
Continue to complete the SDK notepad example. Previously, the functions of the entire example are roughly divided:
1. display the note list;

2. Edit, view, and delete the tabs;

3. Edit the tab title;

4. Real-time folder of the tab Program (desktop shortcut creation)

[Update] free source code: detailed source code for notepad examples under Android SDK

 

In the previous blog, we took the second step. Next we will start the implementation of the third step.

 

3. Edit the tab title

The structure of the table for the notes tab is also seen in the previous blog. The note table contains the title field.

The operation we need is to edit the title of a note ,:

After the title is modified, it is in the list:

Through the above process, we can see the key points of completing the module functions,

1. You need to dynamically add menu items

To implement this function, you still need to return to the noteeditor class.

Public Boolean oncreateoptionsmenu (menu) {super. oncreateoptionsmenu (menu); log. I (TAG, "Enter in noteeditor's oncreateoptionsmenu method"); // build the menus that are shown when editing. if (mstate = state_edit) {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);} // build the menus that are shown when inserting .} else {menu. add (0, discard_id, 0, R. string. menu_discard ). setshortcut ('0', 'D '). seticon (Android. r. drawable. ic_menu_delete);} // [note] Here is what we just added. Use a dynamic method to create a menu item if (! Mnoteonly) {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 ;}

 

According to intent.Category_alternativeFind the qualified activity in inter-filter to complete the operation.

You will findTitleeditorThis activity:

<activity android:name="TitleEditor" android:label="@string/title_edit_title"            android:theme="@android:style/Theme.Dialog" android:windowSoftInputMode="stateVisible">            <!-- This activity implements an alternative action that can be                 performed on notes: editing their title.  It can be used as                 a default operation if the user invokes this action, and is                 available as an alternative action for any note data. -->            <intent-filter android:label="@string/resolve_title">                <!-- This is the action we perform.  It is a custom action we                     define for our application, not a generic VIEW or EDIT                     action since we are not a general note viewer/editor. -->                <action android:name="com.android.notepad.action.EDIT_TITLE" />                <!-- DEFAULT: execute if being directly invoked. -->                <category android:name="android.intent.category.DEFAULT" />                <!-- ALTERNATIVE: show as an alternative action when the user is working with this type of data. -->                <category android:name="android.intent.category.ALTERNATIVE" />                <!-- SELECTED_ALTERNATIVE: show as an alternative action the user can perform when selecting this type of data. -->                <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />                <!-- This is the data type we operate on. -->                <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />            </intent-filter>        </activity>

Note ]:

Note that a problem occurs here. How to display the menu items of menuitem In the effect, that is, how to find the constant in string,

After testing, it is found that it will first find:

<Intent-filter Android: Label = "@ string/resolve_title">

If this label Label cannot be found, it will be searched up. <activity Android: Name = "titleeditor" Android: Label = "@ string/title_edit_title"

The label of the activity is displayed.

 

Test: the picture on the left isIntent-filter has Android: LabelIn this attribute, the right image is to comment out that intent-filter has the Android: Label attribute effect.

2. Modify the title content.

The comparison process should compare the attributes.

/*** This activity is used to edit the tab title and display a floating window containing edittext */public class titleeditor extends activity implements onclicklistener {Private Static final string tag = "titleeditor "; /*** a specific intent action to mark the edit note title operation */public static final string edit_title_action = "com. android. notepad. action. edit_title ";/*** the two fields of the Notes table to be operated on, because the following query uses */Private Static final string [] projection = new string [] {notepad. notes. _ Id, // 0 notepad. notes. title, // 1};/** the title column is in the second column of the note table. The index of the obtained column is 1 */Private Static final int column_index_title = 1; private cursor mcursor; private edittext mtext; private URI muri; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); log. I (TAG, "Enter in titleeditor's oncreate method"); setcontentview (R. layout. title_editor); muri = getintent (). getdat A (); // obtain the URI mcursor = managedquery (muri, projection, null, null) of the Note record to be edited ); // obtain the cursor of the note. // set the event listening mtext = (edittext) this for the edittext and button controls. findviewbyid (R. id. title); mtext. setonclicklistener (this); button B = (button) findviewbyid (R. id. OK); B. setonclicklistener (this) ;}@ override protected void onresume () {super. onresume (); log. I (TAG, "Enter in titleeditor's onresume method"); // in Itialize the text with the title column from the cursor if (mcursor! = NULL) {mcursor. movetofirst (); mtext. settext (mcursor. getstring (column_index_title) ;}@ override protected void onpause () {super. onpause (); log. I (TAG, "Enter in titleeditor's onpause method"); If (mcursor! = NULL) {// write the title back to the note contentvalues values = new contentvalues (); values. put (notes. title, mtext. gettext (). tostring (); getcontentresolver (). update (muri, values, null, null) ;}} public void onclick (view v) {// The activity ends when the user triggers a click event, at this time, the onpause method will be called to keep the current data finish ();}}

 

4. Real-time folder of the Notes Program (desktop shortcut creation)

This effect is amazing. I didn't use it when I just bought a smartphone. Later I found this effect very practical and convenient. The purpose is to quickly read the data information of our program. You will surely know that the corresponding shortcut settings will pop up after you press the screen for a long time.

: From left to right

Analysis: To achieve this effect, we also need to implement the corresponding activity: the corresponding explanations are available in the source code and should be well understood.

Public class noteslivefolder extends activity {/*** the URI for the notes live folder content provider. */public static final URI content_uri = Uri. parse ("content: //" + notepad. authority + "/live_folders/Notes"); public static final URI note_uri = Uri. parse ("content: //" + notepad. authority + "/Notes/#"); @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); Final intent = getintent (); final string action = intent. getaction (); // determines whether to create a real-time folder if (livefolders. action_create_live_folder.equals (Action) {// set the data address final intent livefolderintent = new intent (); livefolderintent. setdata (content_uri); // set the Real-Time folder name, that is, the shortcut title livefolderintent. putextra (livefolders. extra_live_folder_name, getstring (R. string. live_folder_name); // sets the livefolderintent icon of the implementation folder. putextra (livefolders. extra_live_folder_icon, intent. using cuticonresource. fromcontext (this, R. drawable. live_folder_notes); // sets the display mode to list livefolderintent. putextra (livefolders. extra_live_folder_display_mode, livefolders. display_mode_list); // sets the event after clicking. Here, livefolderintent is clicked when one of the tabs is clicked. putextra (livefolders. extra_live_folder_base_intent, new intent (intent. action_edit, note_uri); // The result of this activity shocould be a live folder intent. setresult (result_ OK, livefolderintent);} else {setresult (result_canceled) ;}finish ();}}

 

Another key configuration is as follows:

<! -- After you press on the desktop and select a Real-Time folder, A List dialog box is displayed. If you want to add the activity in your application to this list, you also need to add an intentfilter with the action Android. Intent. Action. create_live_folder when registering the activity. --> <Activity Android: Name = "noteslivefolder" Android: Label = "@ string/live_folder_name" Android: icon = "@ drawable/live_folder_notes"> <intent-filter> <action Android: Name = "android. intent. action. create_live_folder "/> <category Android: Name =" android. intent. category. default "/> </intent-filter> </activity>

 

Note ]:

1. The noteslivefolder activity is triggered only when you click the list icon in the second part of the preceding figure,

Whether the program is displayed in the list is determined by the configuration file. You can refer to the above configuration code.

 

2.

Livefolderintent. putextra (livefolders. extra_live_folder_name, getstring (R. String. live_folder_name ));

Changed:

Livefolderintent. putextra (livefolders.Extra_live_folder_name, "Real-time Notepad ");

:

 

By now, the functions of the notepad example have been fully implemented. By learning, we have actually consolidated a lot of previous knowledge. Come on, and continue to work hard!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.