AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.android.notepad">    <application android:icon="@drawable/app_notes"        android:label="@string/app_name">        <provider android:name="NotePadProvider"            android:authorities="com.google.provider.NotePad"        />        <activity android:name="NotesList" android:label="@string/title_notes_list">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>            <intent-filter>                <action android:name="android.intent.action.VIEW" />                <action android:name="android.intent.action.EDIT" />                <action android:name="android.intent.action.PICK" />                <category android:name="android.intent.category.DEFAULT" />                <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />            </intent-filter>            <intent-filter>                <action android:name="android.intent.action.GET_CONTENT" />                <category android:name="android.intent.category.DEFAULT" />                <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />            </intent-filter>        </activity>                <activity android:name="NoteEditor"            android:theme="@android:style/Theme.Light"            android:label="@string/title_note"            android:screenOrientation="sensor"            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. -->            <intent-filter>                <action android:name="android.intent.action.INSERT" />                <category android:name="android.intent.category.DEFAULT" />                <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />            </intent-filter>        </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>            </application></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
package com.example.android.notepad;import com.example.android.notepad.NotePad.Notes;import android.app.ListActivity;import android.content.ComponentName;import android.content.ContentUris;import android.content.Intent;import android.database.Cursor;import android.net.Uri;import android.os.Bundle;import android.util.Log;import android.view.ContextMenu;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ContextMenu.ContextMenuInfo;import android.widget.AdapterView;import android.widget.ListView;import android.widget.SimpleCursorAdapter;public class NotesList extends ListActivity {    private static final String TAG = "NotesList";    // Menu item ids    public static final int MENU_ITEM_DELETE = Menu.FIRST; //1    public static final int MENU_ITEM_INSERT = Menu.FIRST + 1; //2    /**     * 数据库中感兴趣的列     */    private static final String[] PROJECTION = new String[] {            Notes._ID, // 0 "_id"            Notes.TITLE, // 1 "title"    };    /** 标题列索引 */    private static final int COLUMN_INDEX_TITLE = 1;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);                /*          * public final void setDefaultKeyMode(int mode)         * 设置按键处理快捷方式         */        setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT);                /*         *  创建一个Intent,如果没有数据则使用默认的内容提供商         */        Intent intent = getIntent();        if (intent.getData() == null) {            intent.setData(Notes.CONTENT_URI);        }                /*         *  public ListView getListView()         *  注册 createContextMenu 监听器          */        getListView().setOnCreateContextMenuListener(this);                /*         * 定义游标对象         * public final Cursor managedQuery(Uri uri         * , String[] projection             *   , String selection         *, String[] selectionArgs         *, String sortOrder)         * 执行一个查询,并将结果游标返回         * public Intent getIntent()         * 返回调用此组件的 Intent.         * projection: PROJECTION 列数组         * selection : where         * selectionArgs : 参数         * sortOrder : 排序         *          * 执行一个可管理的查询,并且Activity组件管理查询的关闭和重新查询.         */        Cursor cursor = managedQuery(getIntent().getData(), PROJECTION, null, null,                Notes.DEFAULT_SORT_ORDER);        /*         * 创建一个简单游标适配器, 绑定列与视图         * public SimpleCursorAdapter(Context context         * , int layout         * , Cursor c         * , String[] from         * , int[] to)         * context : 包含ListView的组件         * layout : 包含to的布局文件         * c : 游标         * from : 列数组         * to : 视图id数组         * 映射从数据库到视图的日志条目         */        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.noteslist_item, cursor,                new String[] { Notes.TITLE }, new int[] { android.R.id.text1 });                /*         * public void setListAdapter(ListAdapter adapter)          * 为ListView提供游标         */        setListAdapter(adapter);    }        /*     * 初始化标准选项菜单内容,只在每一次显示前调用.     * 注意,它在onPrepareOptionsMenu之前调用.     * 返回值为真,则显示菜单,否则不显示.     */    @Override    public boolean onCreateOptionsMenu(Menu menu) {        super.onCreateOptionsMenu(menu);        // This is our one standard application action -- inserting a        // new note into the list.        menu.add(0, MENU_ITEM_INSERT, 0, R.string.menu_insert)                .setShortcut('3', 'a')                .setIcon(android.R.drawable.ic_menu_add);        // Generate any additional actions that can be performed on the        // overall list.  In a normal install, there are no additional        // actions found here, but this allows other applications to extend        // our menu with their own actions.        Intent intent = new Intent(null, getIntent().getData());        intent.addCategory(Intent.CATEGORY_ALTERNATIVE);        menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0,                new ComponentName(this, NotesList.class), null, intent, 0, null);        return true;    }        /*     * 准备显示屏幕标准选项.每次菜单显示前调用.可以通过这个方法启用/禁止菜单项或动态修改菜单内容.     * 默认根据Activity状态更新系统菜单项.引申类必须调用基类实现.     * 返回值为真,则显示菜单,否则不显示.     */    @Override    public boolean onPrepareOptionsMenu(Menu menu) {        super.onPrepareOptionsMenu(menu);        final boolean haveItems = getListAdapter().getCount() > 0;        // If there are any notes in the list (which implies that one of        // them is selected), then we need to generate the actions that        // can be performed on the current selection.  This will be a combination        // of our own specific actions along with any extensions that can be        // found.        if (haveItems) {            // This is the selected item.            Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());            // Build menu...  always starts with the EDIT action...            Intent[] specifics = new Intent[1];            specifics[0] = new Intent(Intent.ACTION_EDIT, uri);            MenuItem[] items = new MenuItem[1];            // ... is followed by whatever other actions are available...            Intent intent = new Intent(null, uri);            intent.addCategory(Intent.CATEGORY_ALTERNATIVE);            menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0,                    items);            // Give a shortcut to the edit action.            if (items[0] != null) {                items[0].setShortcut('1', 'e');            }        } else {            menu.removeGroup(Menu.CATEGORY_ALTERNATIVE);        }        return true;    }        /*     * 选项菜单的菜单项 单击回调     */    @Override    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()) {        case MENU_ITEM_INSERT:            // Launch activity to insert a new item            startActivity(new Intent(Intent.ACTION_INSERT, getIntent().getData()));            return true;        }        return super.onOptionsItemSelected(item);    }        /*     * 每次显示上下文菜单前调用.     */    @Override    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {        AdapterView.AdapterContextMenuInfo info;        try {             info = (AdapterView.AdapterContextMenuInfo) menuInfo;        } catch (ClassCastException e) {            Log.e(TAG, "bad menuInfo", e);            return;        }        Cursor cursor = (Cursor) getListAdapter().getItem(info.position);        if (cursor == null) {            // For some reason the requested item isn't available, do nothing            return;        }        // Setup the menu header        menu.setHeaderTitle(cursor.getString(COLUMN_INDEX_TITLE));        // Add a menu item to delete the note        menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_delete);    }        /*     * 上下文菜单的菜单项单击回调     */    @Override    public boolean onContextItemSelected(MenuItem item) {        AdapterView.AdapterContextMenuInfo info;        try {             info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();        } catch (ClassCastException e) {            Log.e(TAG, "bad menuInfo", e);            return false;        }        switch (item.getItemId()) {            case MENU_ITEM_DELETE: {                // Delete the note that the context menu is for                Uri noteUri = ContentUris.withAppendedId(getIntent().getData(), info.id);                getContentResolver().delete(noteUri, null, null);                return true;            }        }        return false;    }        /*     * ListView 列表项单击事件回调     */    @Override    protected void onListItemClick(ListView l, View v, int position, long id) {        Uri uri = ContentUris.withAppendedId(getIntent().getData(), id);                String action = getIntent().getAction();        if (Intent.ACTION_PICK.equals(action) || Intent.ACTION_GET_CONTENT.equals(action)) {            // The caller is waiting for us to return a note selected by            // the user.  The have clicked on one, so return it now.            setResult(RESULT_OK, new Intent().setData(uri));        } else {            // Launch activity to view/edit the currently selected item            startActivity(new Intent(Intent.ACTION_EDIT, uri));        }    }}


菜单分类:
上下文菜单: 不支持快捷方式和图标.
选项菜单: 图标菜单不支持check标记,且菜单标题紧凑显示.最多显示6个,其余显示为more.
子菜单: 不支持图标,不能嵌套子菜单.

更多相关文章

  1. android 右上角菜单无法显示
  2. EditText属性大全
  3. Android(安卓)3.0 r1 API中文文档(106) —— SimpleCursorAdapte
  4. Android(安卓)Q : 安卓源码、水滴屏适配状态栏图标(图标过多时显
  5. Android中使EditText失去焦点,edittext禁止弹出键盘
  6. Android(安卓)中文 API (21) ―― DigitalClock
  7. Android显示调用App内部组件/第三方App内组件
  8. 在Android中如何让gif动起来
  9. android 动态控制状态栏显示和隐藏的方法实例

随机推荐

  1. android Chronometer 时间计时控件 简单
  2. Android第三方授权(新浪微博篇)
  3. Android(安卓)Density
  4. Android(安卓)Studio3.0 Kotlin工程问题
  5. Android(安卓)Studio创建Xposed Module项
  6. Android中对服务器发送http请求
  7. Android动画常用公共属性详解
  8. 已解决:Android向SD卡存储数据时出现java.
  9. 问题Re-installation failed due to diff
  10. Android的SD卡插拔广播