Android 程式開發:(十五)使用菜單 —— 15.3 操作功能表

來源:互聯網
上載者:User

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

如果想要把一個組件與一個操作功能表聯絡在一起,就需要在組件上面調用setOnCreateContextMenuListener()。

下面展示如何顯示一個操作功能表(Context Menu)。

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

[html] 
<?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中添加一些代碼。
[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並維持一段時間後,操作功能表顯出出來了。

 


在上面的例子中,我們調用了button的setOnCreateContextMenuListener()方法,是button與操作功能表關聯到了一起。

當操作功能表中了某一項被點擊時,onContextItemSelected()這個方法就會被觸發。

注意,操作功能表中的快速鍵是不起作用的。如果想要使快速鍵起作用,需要調用setQuertyMode()方法。

[java]
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); 
       } 
       ...... 
   } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.