免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
android中選中菜單的顯示跳轉(zhuǎn)和隱式跳轉(zhuǎn)的實(shí)例介紹

查了好多資料,現(xiàn)發(fā)還是不全,干脆自己整理吧,至少保證在我的做法正確的,以免誤導(dǎo)讀者,也是給自己做個(gè)記錄吧!

簡介

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

options menu就是通過按home鍵來表現(xiàn),context menu需要在view上按上2s后表現(xiàn)。這兩種menu都有可以參加子菜單,子菜單不能種不能嵌套子菜單。options menu最多只能在幕屏最下面表現(xiàn)6個(gè)菜單項(xiàng)選,稱為iconmenu,icon menu不能有checkable項(xiàng)選。多于6的菜單項(xiàng)會(huì)以more icon menu來調(diào)出,稱為expanded menu。options menu通過activity的onCreateOptionsMenu來生成,這個(gè)函數(shù)只會(huì)在menu第一次生成時(shí)用調(diào)。任何想轉(zhuǎn)變options menu的設(shè)法只能在onPrepareOptionsMenu來現(xiàn)實(shí),這個(gè)函數(shù)會(huì)在menu表現(xiàn)前用調(diào)。onOptionsItemSelected 用來理處選中的菜單項(xiàng)。

context menu是跟某個(gè)體具的view綁定在一起,在activity種用registerForContextMenu來為某個(gè)view注冊(cè)context menu。context menu在表現(xiàn)前都市用調(diào)onCreateContextMenu來生成menu。onContextItemSelected用來理處選中的菜單項(xiàng)。  

android還供給了對(duì)菜單項(xiàng)行進(jìn)分組的功能,可以把似相功能的菜單項(xiàng)分紅同一個(gè)組,這樣以可就通過用調(diào)setGroupCheckable,setGroupEnabled,setGroupVisible來設(shè)置菜單屬性,而無須獨(dú)單設(shè)置。

Options Menu

Notepad中使用了options menu和context menu兩種菜單。首先來看生成options menu的onCreateOptionsMenu函數(shù)。 

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

這是一個(gè)標(biāo)準(zhǔn)的插入一個(gè)菜單項(xiàng)的方法,菜單項(xiàng)的id為MENU_ITEM_INSERT。有意思的是下面這幾句代碼: 

復(fù)制代碼 代碼如下:
  
 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); 
  

這到底有何處用呢?其實(shí)這是一種態(tài)動(dòng)菜單技巧(也有點(diǎn)像件插機(jī)制),若某一個(gè)activity,其類型是”android.intent.category.ALTERNATIVE”,據(jù)數(shù)是”vnd.android.cursor.dir/vnd.google.note”的話,系統(tǒng)就會(huì)為這個(gè)activity加增一個(gè)菜單項(xiàng)。在androidmanfest.xml中查看后現(xiàn)發(fā),沒有一個(gè)activity符合條件,所以這段代碼并沒有態(tài)動(dòng)添加出任何一個(gè)菜單項(xiàng)。   

為了驗(yàn)證上述分析,我們可以來做一個(gè)驗(yàn)實(shí),在androidmanfest.xml中行進(jìn)修改,看否是會(huì)態(tài)動(dòng)生成出菜單項(xiàng)。    

驗(yàn)實(shí)一 

      首先我們來建創(chuàng)一個(gè)新的activity作為目標(biāo)activity,名為HelloAndroid,沒有什么功能,就是表現(xiàn)一個(gè)界面。  

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

它所對(duì)應(yīng)的局布界面XML文件如下: 

復(fù)制代碼 代碼如下:
  
<?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滿意上述兩個(gè)條件:

復(fù)制代碼 代碼如下:
  
  <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>
  

好了,行運(yùn)下試試,哎,還是沒有態(tài)動(dòng)菜單項(xiàng)參加呀!怎么回事呢?查看代碼后現(xiàn)發(fā),原來是onPrepareOptionsMenu弄的鬼!這個(gè)函數(shù)在onCreateOptionsMenu后之行運(yùn),下面這段代碼中,由于Menu.CATEGORY_ALTERNATIVE是指向同一個(gè)組,所以把onCreateOptionsMenu中設(shè)置的菜單項(xiàng)給蓋覆掉了,而由于onPrepareOptionsMenu沒有給Menu.CATEGORY_ALTERNATIVE附新值,故Menu.CATEGORY_ALTERNATIVE還是為空。

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

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

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

添加的菜單。因?yàn)閙enu.none也為0。行運(yùn)后以可就看到態(tài)動(dòng)菜單出來了!

下面這個(gè)options menu是在NotesList界面上沒有日記列表選中的情況下生成的,若先選中一個(gè)日記,然后再點(diǎn)”menu”,則生成的options menu是下面這樣的:

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

哎,又態(tài)動(dòng)加增了兩個(gè)菜單項(xiàng)”Edit note”和”Edit title”,這又是如何態(tài)動(dòng)參加的呢?這就是onPrepareOptionsMenu的勞功了。

復(fù)制代碼 代碼如下:
  
    Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());
  

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

復(fù)制代碼 代碼如下:
  
  Intent[] specifics = new Intent[1];
            specifics[0] = new Intent(Intent.ACTION_EDIT, uri);
            MenuItem[] items = new MenuItem[1];
  

然后為選中的日記建創(chuàng)一個(gè)intent,操縱類型為Intent.ACTION_EDIT,據(jù)數(shù)為選中日記的URI.于是會(huì)為選中的日記建創(chuàng)一個(gè)”Edit note”菜單項(xiàng)。

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

這幾句和下面onCreateOptionsMenu函數(shù)中似類,于用態(tài)動(dòng)加增菜單項(xiàng),若某一個(gè)activity,其類型是”android.intent.category.ALTERNATIVE”,據(jù)數(shù)是”vnd.android.cursor.item/vnd.google.note”的話,系統(tǒng)就會(huì)為這個(gè)activity加增一個(gè)菜單項(xiàng)。在androidmanfest.xml中查看后現(xiàn)發(fā),TitleEditor這個(gè)activity符合條件,于是系統(tǒng)就為TitleEditor這個(gè)activity態(tài)動(dòng)添加一個(gè)菜單項(xiàng)”Edit title”。

復(fù)制代碼 代碼如下:
  
else {
            menu.removeGroup(Menu.CATEGORY_ALTERNATIVE);
        }
  

若日記列表為空,則從菜單中除刪組號(hào)為Menu.CATEGORY_ALTERNATIVE的菜單項(xiàng),只剩下”Add note”菜單項(xiàng)。

理處“選中菜單項(xiàng)”事件

菜單項(xiàng)選中事件的理處非常簡略,通過onOptionsItemSelected來成完,這里只是簡略地用調(diào) startActivity(new Intent(Intent.ACTION_INSERT, getIntent().getData()));這個(gè)intent的操縱類型為Intent.ACTION_INSERT,據(jù)數(shù)為日記列表的URI,即”content:// com.google.provider.NotePad/notes”

復(fù)制代碼 代碼如下:
  
     @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函數(shù)現(xiàn)實(shí)。首先確認(rèn)已選中了日記列表中的一個(gè)日記,若沒擇選,則直接返回。Cursor指向選中的日記項(xiàng)。

復(fù)制代碼 代碼如下:
  
   Cursor cursor = (Cursor) getListAdapter().getItem(info.position);
        if (cursor == null) {
            // For some reason the requested item isn't available, do nothing
            return;
        }
  

  然后,設(shè)置上下文菜單的標(biāo)題為日記標(biāo)題

復(fù)制代碼 代碼如下:
  
        // Setup the menu header
        menu.setHeaderTitle(cursor.getString(COLUMN_INDEX_TITLE));

        最后為上下文菜單加增一個(gè)菜單項(xiàng)

  

復(fù)制代碼 代碼如下:
    
        // Add a menu item to delete the note
        menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_delete);

    

  

 對(duì)于上下文菜單項(xiàng)選中的事件理處,是通過重載onContextItemSelected現(xiàn)實(shí)的。

    

復(fù)制代碼 代碼如下:
  
        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;
}

   

   

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

驗(yàn)實(shí)二

來做個(gè)簡略驗(yàn)實(shí),在上述代碼基礎(chǔ)上加增一個(gè)上下文菜單項(xiàng)。首先在onCreateContextMenu函數(shù)中加增一個(gè)上下文菜單項(xiàng):

復(fù)制代碼 代碼如下:
      
menu.add(0,MENU_ITEM_INSERT,0,R.string.menu_insert);
     

      然后為其在onContextItemSelected函數(shù)中加增一個(gè)理處進(jìn)程:

復(fù)制代碼 代碼如下:
      
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;
            }
      

      驗(yàn)實(shí)結(jié)果如下:




本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Android實(shí)例剖析筆記(二)
android開發(fā)學(xué)習(xí)筆記123
使用ContentProvider提供數(shù)據(jù)源(一)ListView中顯示圖片
以及無界面后臺(tái)運(yùn)行服務(wù),開機(jī)啟動(dòng)和殺死服務(wù)后重新啟動(dòng)
Android一些常用知識(shí)和代碼(不斷更新)
Android(經(jīng)典實(shí)例)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服