zoukankan      html  css  js  c++  java
  • Android_notepadz


    新文章移至 
    http://cffile.sinaapp.com/?p=41
     

    AndroidManifest.xml 
    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2.   
    3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    4.     package="com.example.android.notepad">  
    5.   
    6.     <application android:icon="@drawable/app_notes"  
    7.         android:label="@string/app_name">  
    8.   
    9.         <provider android:name="NotePadProvider"  
    10.             android:authorities="com.google.provider.NotePad"  
    11.         />  
    12.   
    13.         <activity android:name="NotesList" android:label="@string/title_notes_list">  
    14.             <intent-filter>  
    15.                 <action android:name="android.intent.action.MAIN" />  
    16.                 <category android:name="android.intent.category.LAUNCHER" />  
    17.             </intent-filter>  
    18.             <intent-filter>  
    19.                 <action android:name="android.intent.action.VIEW" />  
    20.                 <action android:name="android.intent.action.EDIT" />  
    21.                 <action android:name="android.intent.action.PICK" />  
    22.                 <category android:name="android.intent.category.DEFAULT" />  
    23.                 <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />  
    24.             </intent-filter>  
    25.             <intent-filter>  
    26.                 <action android:name="android.intent.action.GET_CONTENT" />  
    27.                 <category android:name="android.intent.category.DEFAULT" />  
    28.                 <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />  
    29.             </intent-filter>  
    30.         </activity>  
    31.           
    32.         <activity android:name="NoteEditor"  
    33.             android:theme="@android:style/Theme.Light"  
    34.             android:label="@string/title_note"  
    35.             android:screenOrientation="sensor"  
    36.             android:configChanges="keyboardHidden|orientation"  
    37.         >  
    38.             <!-- This filter says that we can view or edit the data of  
    39.                  a single note -->  
    40.             <intent-filter android:label="@string/resolve_edit">  
    41.                 <action android:name="android.intent.action.VIEW" />  
    42.                 <action android:name="android.intent.action.EDIT" />  
    43.                 <action android:name="com.android.notepad.action.EDIT_NOTE" />  
    44.                 <category android:name="android.intent.category.DEFAULT" />  
    45.                 <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />  
    46.             </intent-filter>  
    47.   
    48.             <!-- This filter says that we can create a new note inside  
    49.                  of a directory of notes. -->  
    50.             <intent-filter>  
    51.                 <action android:name="android.intent.action.INSERT" />  
    52.                 <category android:name="android.intent.category.DEFAULT" />  
    53.                 <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />  
    54.             </intent-filter>  
    55.   
    56.         </activity>  
    57.           
    58.         <activity android:name="TitleEditor" android:label="@string/title_edit_title"  
    59.                 android:theme="@android:style/Theme.Dialog"  
    60.                 android:windowSoftInputMode="stateVisible">  
    61.             <!-- This activity implements an alternative action that can be  
    62.                  performed on notes: editing their title.  It can be used as  
    63.                  a default operation if the user invokes this action, and is  
    64.                  available as an alternative action for any note data. -->  
    65.             <intent-filter android:label="@string/resolve_title">  
    66.                 <!-- This is the action we perform.  It is a custom action we  
    67.                      define for our application, not a generic VIEW or EDIT  
    68.                      action since we are not a general note viewer/editor. -->  
    69.                 <action android:name="com.android.notepad.action.EDIT_TITLE" />  
    70.                 <!-- DEFAULT: execute if being directly invoked. -->  
    71.                 <category android:name="android.intent.category.DEFAULT" />  
    72.                 <!-- ALTERNATIVE: show as an alternative action when the user is  
    73.                      working with this type of data. -->  
    74.                 <category android:name="android.intent.category.ALTERNATIVE" />  
    75.                 <!-- SELECTED_ALTERNATIVE: show as an alternative action the user  
    76.                      can perform when selecting this type of data. -->  
    77.                 <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />  
    78.                 <!-- This is the data type we operate on. -->  
    79.                 <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />  
    80.             </intent-filter>  
    81.         </activity>  
    82.           
    83.     </application>  
    84. </manifest>  



    程序先检索 
    <intent-filter> 
           <action android:name="android.intent.action.MAIN" /> 
           <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
    的 Activity组件,并根据 android:name="NotesList" 和 package="com.example.android.notepad" 找到 类完全限定路径 com.example.android.notepad.NotesList 类,初始Activity,执行 
    onCreate(Bundle savedInstanceState)方法. 

    NotesList.java 
    Java代码  收藏代码
    1. package com.example.android.notepad;  
    2.   
    3. import com.example.android.notepad.NotePad.Notes;  
    4. import android.app.ListActivity;  
    5. import android.content.ComponentName;  
    6. import android.content.ContentUris;  
    7. import android.content.Intent;  
    8. import android.database.Cursor;  
    9. import android.net.Uri;  
    10. import android.os.Bundle;  
    11. import android.util.Log;  
    12. import android.view.ContextMenu;  
    13. import android.view.Menu;  
    14. import android.view.MenuItem;  
    15. import android.view.View;  
    16. import android.view.ContextMenu.ContextMenuInfo;  
    17. import android.widget.AdapterView;  
    18. import android.widget.ListView;  
    19. import android.widget.SimpleCursorAdapter;  
    20.   
    21.   
    22. public class NotesList extends ListActivity {  
    23.     private static final String TAG = "NotesList";  
    24.   
    25.     // Menu item ids  
    26.     public static final int MENU_ITEM_DELETE = Menu.FIRST; //1  
    27.     public static final int MENU_ITEM_INSERT = Menu.FIRST + 1//2  
    28.   
    29.     /** 
    30.      * 数据库中感兴趣的列 
    31.      */  
    32.     private static final String[] PROJECTION = new String[] {  
    33.             Notes._ID, // 0 "_id"  
    34.             Notes.TITLE, // 1 "title"  
    35.     };  
    36.   
    37.     /** 标题列索引 */  
    38.     private static final int COLUMN_INDEX_TITLE = 1;  
    39.       
    40.     @Override  
    41.     protected void onCreate(Bundle savedInstanceState) {  
    42.         super.onCreate(savedInstanceState);  
    43.           
    44.         /*  
    45.          * public final void setDefaultKeyMode(int mode) 
    46.          * 设置按键处理快捷方式 
    47.          */  
    48.         setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT);  
    49.           
    50.         /* 
    51.          *  创建一个Intent,如果没有数据则使用默认的内容提供商 
    52.          */  
    53.         Intent intent = getIntent();  
    54.         if (intent.getData() == null) {  
    55.             intent.setData(Notes.CONTENT_URI);  
    56.         }  
    57.           
    58.         /* 
    59.          *  public ListView getListView() 
    60.          *  注册 createContextMenu 监听器  
    61.          */  
    62.         getListView().setOnCreateContextMenuListener(this);  
    63.           
    64.         /* 
    65.          * 定义游标对象 
    66.          * public final Cursor managedQuery(Uri uri 
    67.          *                                  , String[] projection 
    68.              *                                  , String selection 
    69.          *                                  , String[] selectionArgs 
    70.          *                                  , String sortOrder) 
    71.          * 执行一个查询,并将结果游标返回 
    72.          * public Intent getIntent() 
    73.          * 返回调用此组件的 Intent. 
    74.          * projection: PROJECTION 列数组 
    75.          * selection : where 
    76.          * selectionArgs : 参数 
    77.          * sortOrder : 排序 
    78.          *  
    79.          * 执行一个可管理的查询,并且Activity组件管理查询的关闭和重新查询. 
    80.          */  
    81.         Cursor cursor = managedQuery(getIntent().getData(), PROJECTION, nullnull,  
    82.                 Notes.DEFAULT_SORT_ORDER);  
    83.         /* 
    84.          * 创建一个简单游标适配器, 绑定列与视图 
    85.          * public SimpleCursorAdapter(Context context 
    86.          *                          , int layout 
    87.          *                          , Cursor c 
    88.          *                          , String[] from 
    89.          *                          , int[] to) 
    90.          * context : 包含ListView的组件 
    91.          * layout : 包含to的布局文件 
    92.          * c : 游标 
    93.          * from : 列数组 
    94.          * to : 视图id数组 
    95.          * 映射从数据库到视图的日志条目 
    96.          */  
    97.         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.noteslist_item, cursor,  
    98.                 new String[] { Notes.TITLE }, new int[] { android.R.id.text1 });  
    99.           
    100.         /* 
    101.          * public void setListAdapter(ListAdapter adapter)  
    102.          * 为ListView提供游标 
    103.          */  
    104.         setListAdapter(adapter);  
    105.     }  
    106.       
    107.     /* 
    108.      * 初始化标准选项菜单内容,只在每一次显示前调用. 
    109.      * 注意,它在onPrepareOptionsMenu之前调用. 
    110.      * 返回值为真,则显示菜单,否则不显示. 
    111.      */  
    112.     @Override  
    113.     public boolean onCreateOptionsMenu(Menu menu) {  
    114.         super.onCreateOptionsMenu(menu);  
    115.   
    116.         // This is our one standard application action -- inserting a  
    117.         // new note into the list.  
    118.         menu.add(0, MENU_ITEM_INSERT, 0, R.string.menu_insert)  
    119.                 .setShortcut('3''a')  
    120.                 .setIcon(android.R.drawable.ic_menu_add);  
    121.   
    122.         // Generate any additional actions that can be performed on the  
    123.         // overall list.  In a normal install, there are no additional  
    124.         // actions found here, but this allows other applications to extend  
    125.         // our menu with their own actions.  
    126.         Intent intent = new Intent(null, getIntent().getData());  
    127.         intent.addCategory(Intent.CATEGORY_ALTERNATIVE);  
    128.         menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 00,  
    129.                 new ComponentName(this, NotesList.class), null, intent, 0null);  
    130.   
    131.         return true;  
    132.     }  
    133.       
    134.     /* 
    135.      * 准备显示屏幕标准选项.每次菜单显示前调用.可以通过这个方法启用/禁止菜单项或动态修改菜单内容. 
    136.      * 默认根据Activity状态更新系统菜单项.引申类必须调用基类实现. 
    137.      * 返回值为真,则显示菜单,否则不显示. 
    138.      */  
    139.     @Override  
    140.     public boolean onPrepareOptionsMenu(Menu menu) {  
    141.         super.onPrepareOptionsMenu(menu);  
    142.         final boolean haveItems = getListAdapter().getCount() > 0;  
    143.   
    144.         // If there are any notes in the list (which implies that one of  
    145.         // them is selected), then we need to generate the actions that  
    146.         // can be performed on the current selection.  This will be a combination  
    147.         // of our own specific actions along with any extensions that can be  
    148.         // found.  
    149.         if (haveItems) {  
    150.             // This is the selected item.  
    151.             Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());  
    152.   
    153.             // Build menu...  always starts with the EDIT action...  
    154.             Intent[] specifics = new Intent[1];  
    155.             specifics[0] = new Intent(Intent.ACTION_EDIT, uri);  
    156.             MenuItem[] items = new MenuItem[1];  
    157.   
    158.             // ... is followed by whatever other actions are available...  
    159.             Intent intent = new Intent(null, uri);  
    160.             intent.addCategory(Intent.CATEGORY_ALTERNATIVE);  
    161.             menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 00null, specifics, intent, 0,  
    162.                     items);  
    163.   
    164.             // Give a shortcut to the edit action.  
    165.             if (items[0] != null) {  
    166.                 items[0].setShortcut('1''e');  
    167.             }  
    168.         } else {  
    169.             menu.removeGroup(Menu.CATEGORY_ALTERNATIVE);  
    170.         }  
    171.   
    172.         return true;  
    173.     }  
    174.       
    175.     /* 
    176.      * 选项菜单的菜单项 单击回调 
    177.      */  
    178.     @Override  
    179.     public boolean onOptionsItemSelected(MenuItem item) {  
    180.         switch (item.getItemId()) {  
    181.         case MENU_ITEM_INSERT:  
    182.             // Launch activity to insert a new item  
    183.             startActivity(new Intent(Intent.ACTION_INSERT, getIntent().getData()));  
    184.             return true;  
    185.         }  
    186.         return super.onOptionsItemSelected(item);  
    187.     }  
    188.       
    189.     /* 
    190.      * 每次显示上下文菜单前调用. 
    191.      */  
    192.     @Override  
    193.     public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {  
    194.         AdapterView.AdapterContextMenuInfo info;  
    195.         try {  
    196.              info = (AdapterView.AdapterContextMenuInfo) menuInfo;  
    197.         } catch (ClassCastException e) {  
    198.             Log.e(TAG, "bad menuInfo", e);  
    199.             return;  
    200.         }  
    201.   
    202.         Cursor cursor = (Cursor) getListAdapter().getItem(info.position);  
    203.         if (cursor == null) {  
    204.             // For some reason the requested item isn't available, do nothing  
    205.             return;  
    206.         }  
    207.   
    208.         // Setup the menu header  
    209.         menu.setHeaderTitle(cursor.getString(COLUMN_INDEX_TITLE));  
    210.   
    211.         // Add a menu item to delete the note  
    212.         menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_delete);  
    213.     }  
    214.       
    215.     /* 
    216.      * 上下文菜单的菜单项单击回调 
    217.      */  
    218.     @Override  
    219.     public boolean onContextItemSelected(MenuItem item) {  
    220.         AdapterView.AdapterContextMenuInfo info;  
    221.         try {  
    222.              info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();  
    223.         } catch (ClassCastException e) {  
    224.             Log.e(TAG, "bad menuInfo", e);  
    225.             return false;  
    226.         }  
    227.   
    228.         switch (item.getItemId()) {  
    229.             case MENU_ITEM_DELETE: {  
    230.                 // Delete the note that the context menu is for  
    231.                 Uri noteUri = ContentUris.withAppendedId(getIntent().getData(), info.id);  
    232.                 getContentResolver().delete(noteUri, nullnull);  
    233.                 return true;  
    234.             }  
    235.         }  
    236.         return false;  
    237.     }  
    238.       
    239.     /* 
    240.      * ListView 列表项单击事件回调 
    241.      */  
    242.     @Override  
    243.     protected void onListItemClick(ListView l, View v, int position, long id) {  
    244.         Uri uri = ContentUris.withAppendedId(getIntent().getData(), id);  
    245.           
    246.         String action = getIntent().getAction();  
    247.         if (Intent.ACTION_PICK.equals(action) || Intent.ACTION_GET_CONTENT.equals(action)) {  
    248.             // The caller is waiting for us to return a note selected by  
    249.             // the user.  The have clicked on one, so return it now.  
    250.             setResult(RESULT_OK, new Intent().setData(uri));  
    251.         } else {  
    252.             // Launch activity to view/edit the currently selected item  
    253.             startActivity(new Intent(Intent.ACTION_EDIT, uri));  
    254.         }  
    255.     }  
    256. }  


    菜单分类: 
    上下文菜单: 不支持快捷方式和图标. 
    选项菜单: 图标菜单不支持check标记,且菜单标题紧凑显示.最多显示6个,其余显示为more. 
    子菜单: 不支持图标,不能嵌套子菜单.
  • 相关阅读:
    严重: Parse error in application web.xml file at jndi:/localhost/ipws/WEBINF/web.xml java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml
    Failed to install .apk on device 'emulator5554': timeout解决方法
    java.lang.NoClassDefFoundError:org.jsoup.Jsoup
    Conversion to Dalvik format failed: Unable to execute dex:解决方法
    apache Digest: generating secret for digest authentication ...
    Description Resource Path Location Type Project has no default.properties file! Edit the project properties to set one.
    android service随机自启动
    MVC3 安装部署
    EF 4.3 CodeBased 数据迁移演练
    SQL Server 2008开启sa账户
  • 原文地址:https://www.cnblogs.com/chenzhihong/p/2232223.html
Copyright © 2011-2022 走看看