Android, How to create Context Menu…

☆樱花仙子☆ 提交于 2019-12-12 11:00:00

问题


Here i wrote some code but not getting output.. Please tell me why is not displaying that context menu, where am i doing mistake...? Please guide me, Thanks in Advance....

more_tab_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/feeds"
    android:title="Feeds"/>
<item
    android:id="@+id/friends"
    android:title="Friends"/>
<item
    android:id="@+id/about"
    android:title="About"/>
</menu>

MenuTest.java

public class MenuTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater =getMenuInflater();
    inflater.inflate(R.menu.more_tab_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo contextMenuInfo=(AdapterContextMenuInfo)item.getMenuInfo();
    switch(item.getItemId())
    {
    case R.id.feeds:
        break;
    case R.id.friends:
        break;
    case R.id.about:
        break;
    }

    return super.onContextItemSelected(item);
}
}

Please tell me where am i doing mistake...?


回答1:


Right now you have this:

super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater =getMenuInflater();
inflater.inflate(R.menu.more_tab_menu, menu);

Change it to this:

MenuInflater inflater =getMenuInflater();
inflater.inflate(R.menu.more_tab_menu, menu);
return true;

Also in onOptionsItemSelected:

return true;

Also use onCreateOptionsMenu and onOptionsItemSelected.




回答2:


You need to register your menu with registerForContextMenu.

From this page

In order for a View to provide a context menu, you must "register" the view for a context menu. Call registerForContextMenu() and pass it the View you want to give a context menu. When this View then receives a long-press, it displays a context menu.

Your code above works just fine. You just need to register the content menu to a view.

If you want to launch the context menu from anywhere in the screen:

Let's say your layout main.xml is like the following:

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

</LinearLayout>

You will register the context menu you have created with the following (in onCreate):

 LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
 registerForContextMenu(layout);

So if you run this in the emulator, and do a long-click on the Android desktop, your menu will pop up.




回答3:


Replace this:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.more_tab_menu, menu);
}    

With this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.more_tab_menu, menu);

    return true;
}

This will result in the menu items being displayed when the Menu button on the phone is pressed.



来源:https://stackoverflow.com/questions/6439624/android-how-to-create-context-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!