本文介绍了Android的,如何创建上下文菜单...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我写了一些code,但没有得到输出..
请告诉我为什么不显示该上下文菜单,我在哪里做的错误......?
请指引我,先谢谢了......

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

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

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...?

推荐答案

现在你有这样的:

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

改成这样:

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

此外,在onOptionsItemSelected:

Also in onOptionsItemSelected:

return true;

也可以使用onCreateOptionsMenu和onOptionsItemSelected。

Also use onCreateOptionsMenu and onOptionsItemSelected.

这篇关于Android的,如何创建上下文菜单...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 19:13