如何动态创建菜单项? [英] How can I dynamically create menu items?

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

问题描述

我要建一个Android应用程序,我试图建立一个用户管理系统,用户可以登录,注销,等我想显示登录菜单项,如果用户注销和注销按钮,如果用户登录,我该怎么做动态?

这是布局文件现在:

 < XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
  <项目机器人:ID =@ + ID /加机器人:标题=添加安卓图标=@可绘制/ ic_menu_add/>
  <项目机器人:ID =@ + ID /列表机器人:标题=列表安卓图标=@可绘制/ ic_menu_list/>
  <项目机器人:ID =@ + ID /刷新机器人:标题=刷新安卓图标=@可绘制/ ic_menu_refresh/>
  <项目机器人:ID =@ + ID /密码机器人:标题=登录安卓图标=@可绘制/ ic_menu_login/>
< /菜单>
 

这是我的Java现在:

  @覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    新MenuInflater(本).inflate(R.menu.activity_main,菜单);
    返程(super.onCreateOptionsMenu(菜单));
}

@覆盖
公共布尔onOptionsItemSelected(菜单项项)
{
    的System.out.println(item.getItemId()== R.id.add);
    如果(item.getItemId()== R.id.add)
    {
        除非我们已经获得了用户的当前位置//无法添加点。
        如果((currentLat = 0)及!&安培;!(currentLng = 0))
        {

            的System.out.println(的loggedIn:+ auth.isLoggedIn());
            如果(!auth.isLoggedIn())
            {
                Toast.makeText(MainActivity.this,你必须先登录才能添加新的点,
                        Toast.LENGTH_LONG).show();
            }
            其他
            {


                意图addIntent =新的意图(MainActivity.this,AddSpot.class);
                叠B =新包();
                b.putDouble(currentLat,currentLat);
                b.putDouble(currentLng,currentLng);
                addIntent.putExtras(B);
                startActivity(addIntent);
                返回(真);
            }
        }
    }
    否则,如果(item.getItemId()== R.id.list)
    {
        //毫无意义的向他们展示一个空白屏幕如果不从服务器中检索
        如果(名单!= NULL)
        {
            意图listIntent =新的意图(MainActivity.this,ListLocations.class);
            listIntent.putExtra(名单,名单);
            startActivity(listIntent);
            返回(真);
        }
    }

    如果(item.getItemId()== R.id.refresh)
    {
        完();
        startActivity(getIntent());
        返回(真);
    }

    如果(item.getItemId()== R.id.login)
    {
        意图loginIntent =新的意图(MainActivity.this,LoginActivity.class);
        startActivity(loginIntent);
        返回(真);
    }

    返程(super.onOptionsItemSelected(项目));
}
 

解决方案

如何动态添加菜单项,以一个Android活动

 公共类yourActivity延伸活动{
    ...
    私有静态最终诠释MENU_ADD = Menu.FIRST;
    私有静态最终诠释MENU_LIST = MENU.FIRST + 1;
    私有静态最终诠释MENU_REFRESH = MENU.FIRST + 2;
    私有静态最终诠释MENU_LOGIN = MENU.FIRST + 3;

    / **
     *使用,如果你的菜单是静态的(即不变)
     * /
    / *
    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        super.onCreateOptionsMenu(菜单);
        menu.add(0,MENU_ADD,Menu.NONE,R.string.your  - 加文).setIcon(R.drawable.your-添加图标);
        menu.add(0,MENU_LIST,Menu.NONE,R.string.your列表文本).setIcon(R.drawable.your列表图标);
        menu.add(0,MENU_REFRESH,Menu.NONE,R.string.your刷新文本).setIcon(R.drawable.your刷新图标);
        menu.add(0,MENU_LOGIN,Menu.NONE,R.string.your登陆文本).setIcon(R.drawable.your登陆图标);
        返回true;
    }
    * /

    / **
     *被调用每一个用户presses菜​​单按钮的时间。
     *使用,如果你的菜单是动态的。
     * /
    @覆盖
    公共布尔prepareOptionsMenu(功能菜单)在{
        menu.clear();
        如果(enableAdd)
            menu.add(0,MENU_ADD,Menu.NONE,R.string.your  - 加文).setIcon(R.drawable.your-添加图标);
        如果(enableList)
            menu.add(0,MENU_LIST,Menu.NONE,R.string.your列表文本).setIcon(R.drawable.your列表图标);
        如果(enableRefresh)
            menu.add(0,MENU_REFRESH,Menu.NONE,R.string.your刷新文本).setIcon(R.drawable.your刷新图标);
        如果(enableLogin)
            menu.add(0,MENU_LOGIN,Menu.NONE,R.string.your登陆文本).setIcon(R.drawable.your登陆图标);
        返回super.on prepareOptionsMenu(菜单);
    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        super.onOptionsItemSelected(项目);

        开关(item.getItemId()){
        案MENU_ADD:doAddStuff();打破;
        案MENU_LIST:doListStuff();打破;
        案MENU_REFRESH:doRefreshStuff();打破;
        案MENU_LOGIN:doLoginStuff();打破;
        }
        返回false;
    }
 


下面的具体示例将在用户登录的MENU_LOGOUT选项。

 私有静态最终诠释MENU_LOGOUT = MENU.FIRST + 4;

    公共布尔prepareOptionsMenu(功能菜单)在{
        ...
        如果(auth.isLoggedIn()){
            menu.add(0,MENU_LOGOUT,Menu.NONE,R.string.your-注销文本).setIcon(R.drawable.your-注销图标);
        }
        ...
    }

    公共布尔onOptionsItemSelected(菜单项项){
        ...
        案例MENU_LOGOUT:
            如果(auth.isLoggedIn()){
                doLogout();
            } 其他 {
                Toast.makeText(这一点,你一定是莫名其妙地被注销菜单按钮被pressed和现在之间的时间。,Toast.DURATION_LONG).show();
            }
            打破;
        ...
    }
 

这是所有有给它。

I'm building an Android application and I'm trying to build a user management system where users can login, logout, etc. I want to display a login menu item if the user is logged out and a logout button if the user is logged in. How can I do this dynamically?

This is the layout file right now:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/add" android:title="Add" android:icon="@drawable/ic_menu_add"/>
  <item android:id="@+id/list" android:title="List" android:icon="@drawable/ic_menu_list"/>
  <item android:id="@+id/refresh" android:title="Refresh" android:icon="@drawable/ic_menu_refresh"/>
  <item android:id="@+id/login" android:title="Login" android:icon="@drawable/ic_menu_login"/>
</menu>

This is my Java right now:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(this).inflate(R.menu.activity_main, menu);
    return(super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    System.out.println(item.getItemId()==R.id.add);
    if (item.getItemId()==R.id.add)
    {
        //Cannot add spot unless we have obtained the users current location. 
        if((currentLat != 0) && (currentLng != 0))
        {

            System.out.println("loggedin? : "  + auth.isLoggedIn());
            if(!auth.isLoggedIn())
            {
                Toast.makeText(MainActivity.this, "You must be logged in to add a new spot",
                        Toast.LENGTH_LONG).show();
            }
            else
            {


                Intent addIntent = new Intent(MainActivity.this, AddSpot.class);
                Bundle b = new Bundle();
                b.putDouble("currentLat", currentLat);
                b.putDouble("currentLng", currentLng);
                addIntent.putExtras(b);
                startActivity(addIntent);
                return(true);
            }
        }
    }   
    else if(item.getItemId()==R.id.list)
    {
        //Pointless showing them a blank screen if nothing is retrieved from the server
        if(list != null)
        {
            Intent listIntent = new Intent(MainActivity.this, ListLocations.class);
            listIntent.putExtra("list", list);
            startActivity(listIntent);
            return(true);
        }
    }

    if(item.getItemId()==R.id.refresh)
    {
        finish();
        startActivity(getIntent());
        return(true);       
    }

    if(item.getItemId()==R.id.login)
    {
        Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(loginIntent);
        return(true);   
    }

    return(super.onOptionsItemSelected(item));
}

解决方案

How to Dynamically Add Menu Items to an Android Activity

public class yourActivity extends Activity {
    ...
    private static final int MENU_ADD = Menu.FIRST;
    private static final int MENU_LIST = MENU.FIRST + 1;
    private static final int MENU_REFRESH = MENU.FIRST + 2;
    private static final int MENU_LOGIN = MENU.FIRST + 3;

    /**
     * Use if your menu is static (i.e. unchanging)
     */
    /*
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon);
        menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon);
        menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon);
        menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon);
        return true;
    }
    */

    /**
     * Gets called every time the user presses the menu button.
     * Use if your menu is dynamic.
     */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menu.clear();
        if(enableAdd)
            menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon);
        if(enableList)
            menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon);
        if(enableRefresh)
            menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon);
        if(enableLogin)
            menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);

        switch (item.getItemId()) {
        case MENU_ADD: doAddStuff(); break;
        case MENU_LIST: doListStuff(); break;
        case MENU_REFRESH: doRefreshStuff(); break;
        case MENU_LOGIN: doLoginStuff(); break;
        }
        return false;
    }


The following specific example adds a MENU_LOGOUT option if the user is logged in.

    private static final int MENU_LOGOUT = MENU.FIRST + 4;

    public boolean onPrepareOptionsMenu(Menu menu) {
        ...
        if(auth.isLoggedIn()) {
            menu.add(0, MENU_LOGOUT, Menu.NONE, R.string.your-logout-text).setIcon(R.drawable.your-logout-icon);
        }
        ...
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        ...
        case MENU_LOGOUT:
            if(auth.isLoggedIn()) {
                doLogout();
            } else {
                Toast.makeText(this, "You must have somehow been logged out between the time the menu button was pressed and now.", Toast.DURATION_LONG).show();
            }
            break;
        ...
    }

That's all there is to it.

这篇关于如何动态创建菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆