android中選中菜單的顯示跳轉和隱式跳轉的執行個體介紹

來源:互聯網
上載者:User

查了好多資料,現發還是不全,乾脆自己整理吧,至少保證在我的做法正確的,以免誤導讀者,也是給自己做個記錄吧!

簡介

android供給了三種菜單類型,分別為options menu,context menu,sub menu。

options menu就是通過按home鍵來表現,context menu需要在view上按上2s後表現。這兩種menu都有可以參加子功能表,子功能表不能種不能嵌套子功能表。options menu最多隻能在幕屏最下面表現6個功能表項目選,稱為iconmenu,icon menu不能有checkable項選。多於6的功能表項目會以more icon menu來調出,稱為expanded menu。options menu通過activity的onCreateOptionsMenu來產生,這個函數只會在menu第一次產生時用調。任何想轉變options menu的設法只能在onPrepareOptionsMenu來現實,這個函數會在menu表現前用調。onOptionsItemSelected 用來理處選中的功能表項目。

context menu是跟某個體具的view綁定在一起,在activity種用registerForContextMenu來為某個view註冊context menu。context menu在表現前都市用調onCreateContextMenu來產生menu。onContextItemSelected用來理處選中的功能表項目。

android還供給了對功能表項目行進分組的功能,可以把似相功能的功能表項目分紅同一個組,這樣以可就通過用調setGroupCheckable,setGroupEnabled,setGroupVisible來設定菜單屬性,而無須獨單設定。

Options Menu

Notepad中使用了options menu和context menu兩種菜單。首先來看產生options menu的onCreateOptionsMenu函數。

複製代碼 代碼如下: menu.add(0, MENU_ITEM_INSERT, 0, R.string.menu_insert)
.setShortcut('3', 'a')
.setIcon(android.R.drawable.ic_menu_add);

這是一個標準的插入一個功能表項目的方法,功能表項目的id為MENU_ITEM_INSERT。有意思的是下面這幾句代碼:

複製代碼 代碼如下: 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);

這到底有何處用呢?其實這是一種態動菜單技巧(也有點像件插機制),若某一個activity,其類型是”android.intent.category.ALTERNATIVE”,據數是”vnd.android.cursor.dir/vnd.google.note”的話,系統就會為這個activity加增一個功能表項目。在androidmanfest.xml中查看後現發,沒有一個activity符合條件,所以這段代碼並沒有態動添加出任何一個功能表項目。

為了驗證上述分析,我們可以來做一個驗實,在androidmanfest.xml中行進修改,看否是會態動產生出功能表項目。

驗實一

首先我們來建創一個新的activity作為目標activity,名為HelloAndroid,沒有什麼功能,就是表現一個介面。

複製代碼 代碼如下:public class HelloAndroid extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
}
}

它所對應的局布介面XML檔案如下:

複製代碼 代碼如下:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/TextView01"/>

<Button android:id="@+id/Button01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/txtInfo"></Button>
</LinearLayout>

然後修改androidmanfest.xml,參加下面這段配置,讓HelloAndroid滿意上述兩個條件:

複製代碼 代碼如下: <activity android:name="HelloAndroid" android:label="@string/txtInfo">
<intent-filter>
<action android:name="com.android.notepad.action.HELLO_TEST" />
<category android:name="android.intent.category.ALTERNATIVE"/>
<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
</intent-filter>
</activity>

好了,行運下試試,哎,還是沒有態動功能表項目參加呀!怎麼回事呢?查看代碼後現發,原來是onPrepareOptionsMenu弄的鬼!這個函數在onCreateOptionsMenu後之行運,下面這段代碼中,由於Menu.CATEGORY_ALTERNATIVE是指向同一個組,所以把onCreateOptionsMenu中設定的功能表項目給蓋覆掉了,而由於onPrepareOptionsMenu沒有給Menu.CATEGORY_ALTERNATIVE附新值,故Menu.CATEGORY_ALTERNATIVE還是為空白。

複製代碼 代碼如下: Intent intent = new Intent(null, uri);
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0,items);

好的,那我們臨時把下面這幾句給釋注掉,當然,也可以不釋注這幾句,在onCreateOptionsMenu中改groupid號,即將Menu.CATEGORY_ALTERNATIVE為改Menu.first,其他的也行,但意注不要為改menu.none,這樣會蓋覆掉。

複製代碼 代碼如下:menu.add(0, MENU_ITEM_INSERT, 0, R.string.menu_insert)
.setShortcut('3', 'a')
.setIcon(android.R.drawable.ic_menu_add);

添加的菜單。因為menu.none也為0。行運後以可就看到態動菜單出來了!

下面這個options menu是在NotesList介面上沒有日記列表選中的情況下產生的,若先選中一個日記,然後再點”menu”,則產生的options menu是下面這樣的:

每日一道理
一個安靜的夜晚,我獨自一人,有些空虛,有些淒涼。坐在星空下,抬頭仰望美麗天空,感覺真實卻由虛幻,閃閃爍爍,似乎看來還有些跳動。美的一切總在瞬間,如同“海市蜃樓”般,也只是刹那間的一閃而過,當天空變得明亮,而這星星也早已一同退去……

哎,又態動加增了兩個功能表項目”Edit note”和”Edit title”,這又是如何態動參加的呢?這就是onPrepareOptionsMenu的勞功了。

複製代碼 代碼如下: Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());

首先獲得選中的日記(若沒有擇選,則uri為空白)

複製代碼 代碼如下: Intent[] specifics = new Intent[1];
specifics[0] = new Intent(Intent.ACTION_EDIT, uri);
MenuItem[] items = new MenuItem[1];

然後為選中的日記建創一個intent,操縱類型為Intent.ACTION_EDIT,據數為選中日記的URI.於是會為選中的日記建創一個”Edit note”功能表項目。

複製代碼 代碼如下: Intent intent = new Intent(null, uri);
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0,
items);

這幾句和下面onCreateOptionsMenu函數中似類,於用態動加增功能表項目,若某一個activity,其類型是”android.intent.category.ALTERNATIVE”,據數是”vnd.android.cursor.item/vnd.google.note”的話,系統就會為這個activity加增一個功能表項目。在androidmanfest.xml中查看後現發,TitleEditor這個activity符合條件,於是系統就為TitleEditor這個activity態動添加一個功能表項目”Edit title”。

複製代碼 代碼如下:else {
menu.removeGroup(Menu.CATEGORY_ALTERNATIVE);
}

若日記列表為空白,則從菜單中除刪組號為Menu.CATEGORY_ALTERNATIVE的功能表項目,只剩下”Add note”功能表項目。

理處“選中功能表項目”事件

功能表項目選中事件的理處非常簡略,通過onOptionsItemSelected來成完,這裡只是簡略地用調 startActivity(new Intent(Intent.ACTION_INSERT, getIntent().getData()));這個intent的操縱類型為Intent.ACTION_INSERT,據數為日記列表的URI,即”content:// com.google.provider.NotePad/notes”

複製代碼 代碼如下: @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);
}

Context Menu

下面分析另一種菜單---操作功能表,這通過重載onCreateContextMenu函數現實。首先確認已選中了日記列表中的一個日記,若沒擇選,則直接返回。Cursor指向選中的日記項。

複製代碼 代碼如下: 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);

對於操作功能表項目選中的事件理處,是通過重載onContextItemSelected現實的。

複製代碼 代碼如下: 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;
}

對於日記的除刪,首先用調ContentUris.withAppendedId(getIntent().getData(), info.id);來接拼出待除刪日記的URI.然後getContentResolver().delete(noteUri, null, null);用調層下的Content Provider去除刪此日記。

驗實二

來做個簡略驗實,在上述代碼基礎上加增一個操作功能表項目。首先在onCreateContextMenu函數中加增一個操作功能表項目:

複製代碼 代碼如下:menu.add(0,MENU_ITEM_INSERT,0,R.string.menu_insert);

然後為其在onContextItemSelected函數中加增一個理處進程:

複製代碼 代碼如下:case MENU_ITEM_INSERT:
{
new AlertDialog.Builder(this).setIcon(R.drawable.app_notes)
.setTitle(R.string.app_name).setMessage(R.string.error_message).setPositiveButton(R.string.button_ok, new OnClickListener(){

public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}

}).show();
return true;
}

驗實結果如下:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.