如何使用菜单项上的点击监听器?

时间:2016-07-23 12:11:06

标签: android android-menu

我不想使用toast,而是想在菜单项中使用click监听器,在这种情况下我们可以使用碎片 这是我想在点击上添加的以下代码,因此我可以在新活动中打开

//启动菜单XML文件(menu.xml)

 @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        return true;
    }

    /**
     * Event Handling for Individual menu item selected
     * Identify single menu item by it's id
     * */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {

        switch (item.getItemId())
        {
        case R.id.menu_bookmark:
            // Single menu item is selected do something
            // Ex: launching new activity/screen or show alert message
            Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_save:
            Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_search:
            Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_share:
            Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_delete:
            Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
            return true;

        case R.id.menu_preferences:
            Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }    

}

2 个答案:

答案 0 :(得分:1)

是的,您可以通过简单地在交换机案例中为每个菜单项添加一个Intent来实现。请查看以下代码段以供参考:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{

    switch (item.getItemId())
    {
    case R.id.menu_bookmark:

        // Ex: launching new activity/screen or show alert message

        Intent intent = new Intent(yourActivity.this, NextActivity.class)
        startActivity(intent);
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}    

答案 1 :(得分:0)

@Override
public boolean onOptionsItemSelected(MenuItem item)
{

    switch (item.getItemId())
    {
    case R.id.menu_bookmark:

        Intent intent = new Intent(currentactivity.this, toactivity.class)
        startActivity(intent);

        return true;

    case R.id.menu_save:
        Intent intent = new Intent(currentactivity.this, toactivity.class)
        startActivity(intent);
        return true;

    case R.id.menu_search:
        Intent intent = new Intent(currentactivity.this, toactivity.class)
        startActivity(intent);
        return true;

    case R.id.menu_share:
        Intent intent = new Intent(currentactivity.this, toactivity.class)
        startActivity(intent);
        return true;

    case R.id.menu_delete:
        Intent intent = new Intent(currentactivity.this, toactivity.class)
        startActivity(intent);
        return true;

    case R.id.menu_preferences:
        Intent intent = new Intent(currentactivity.this, toactivity.class)
        startActivity(intent);
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}    

<强>意图 您必须使用意图从一个屏幕移动到另一个屏幕

    Intent intent = new Intent(currentactivity.this,towhichactivityyouwantmove.class) 
    startActivity(intent);