麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統(tǒng) > Android > 正文

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

2020-04-11 12:24:46
字體:
供稿:網(wǎng)友

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

簡(jiǎn)介

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)選中事件的理處非常簡(jiǎn)略,通過onOptionsItemSelected來成完,這里只是簡(jiǎn)略地用調(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è)簡(jiǎn)略驗(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é)果如下:
選中和菜單

選中和菜單

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产无遮挡一区二区三区毛片日本 | 国产精品久久久久久久久久久久久久久 | omofun 动漫在线观看 | 国产在线一级片 | 老女人碰碰在线碰碰视频 | 蜜桃传媒视频麻豆第一区免费观看 | 国产在线观看91一区二区三区 | 天天透天天狠天天爱综合97 | 欧美在线a | 中国精品久久 | 国产成人在线视频播放 | 久久久久一本一区二区青青蜜月 | 在线视频 中文字幕 | 最新一区二区三区 | 国产午夜精品久久久久 | 狠狠干天天操 | 国产高潮失禁喷水爽到抽搐视频 | 久久精品99久久久久久2456 | 国产午夜精品一区二区三区视频 | 女人解衣喂奶电影 | 日韩欧美激情视频 | 偿还的影视高清在线观看 | 美女亚洲综合 | 欧美精品成人一区二区在线观看 | 精品一区二区久久久久久久网精 | 久久免费观看一级毛片 | 羞羞视频2023 | 久久不射电影网 | 亚洲国产高清视频 | 色综合久久久久综合99 | 国产一级在线观看视频 | 毛片免费看的 | 久久蜜桃精品一区二区三区综合网 | 欧美韩国一区 | av视在线 | 黄色香蕉视频 | 粉嫩粉嫩一区二区三区在线播放 | 韩国一级免费视频 | 亚洲一区二区免费视频 | 免费观看一级黄色片 | xxxeexxx性国产 |