日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

【Android 開發教程】上下文菜單

系統 2113 0

本章節翻譯自《Beginning-Android-4-Application-Development》,如有翻譯不當的地方,敬請指出。

原書購買地址 http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


在上一節,我們已經知道了在按MENU鍵的時候,如何顯示選項菜單。但是,除了選項菜單,你也可以顯示一個上下文菜單。上下文菜單通常是和activity中的組件相關聯的。當用戶長按一個組件的時候,它的上下文菜單就會被觸發。例如,用戶長按一個Button,一個上下文菜單就有可能被顯示。

如果想要把一個組件與一個上下文菜單聯系在一起,就需要在組件上面調用setOnCreateContextMenuListener()。

下面展示如何顯示一個上下文菜單(Context Menu)。

1. 使用之前的工程,修改main.xml。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click and hold on it" />

</LinearLayout>
  

2. 在MenusActivity.java中添加一些代碼。

    public class MenusActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnCreateContextMenuListener(this);
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View view,
    ContextMenuInfo menuInfo)
    {
         super.onCreateContextMenu(menu, view, menuInfo);
         CreateMenu(menu);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        ......
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
         return MenuChoice(item);
    }
    
    private void CreateMenu(Menu menu)
    {
        ......  
    }

    private boolean MenuChoice(MenuItem item)
    {
        ......
    }

}
  

3. 按F11在模擬器上面調試。當點擊Button并維持一段時間后,上下文菜單顯出出來了。

【Android 開發教程】上下文菜單


在上面的例子中,我們調用了button的setOnCreateContextMenuListener()方法,是button與上下文菜單關聯到了一起。

當上下文菜單中了某一項被點擊時,onContextItemSelected()這個方法就會被觸發。

注意,上下文菜單中的快捷鍵是不起作用的。如果想要使快捷鍵起作用,需要調用setQuertyMode()方法。

     private void CreateMenu(Menu menu)
    {
        menu.setQwertyMode(true);
        MenuItem mnu1 = menu.add(0, 0, 0, "Item 1");
        {
            mnu1.setAlphabeticShortcut('a');
            mnu1.setIcon(R.drawable.ic_launcher);
        }
        ......
    }

  



【Android 開發教程】上下文菜單


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 麻栗坡县| 金寨县| 正宁县| 平阴县| 且末县| 东城区| 扎赉特旗| 崇阳县| 施秉县| 金秀| 延川县| 阳高县| 张家口市| 疏勒县| 长沙市| 凤阳县| 沿河| 遵义县| 巴彦淖尔市| 辽阳市| 龙泉市| 扎囊县| 马鞍山市| 六枝特区| 太和县| 横峰县| 常山县| 廉江市| 景宁| 大连市| 民乐县| 昌吉市| 若羌县| 香格里拉县| 嘉义县| 定日县| 兴山县| 仪征市| 民和| 石门县| 寻乌县|