将不同的上下文菜单分配给按钮数

时间:2011-12-31 08:00:01

标签: java android button contextmenu

我有一个带有几个按钮的窗口。我希望当单击一个按钮向我显示上下文菜单时,如果按下其他按钮,则另一个上下文菜单会在上下文菜单中显示其他内容。 这是我的代码

package com.a.c;    

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.Toast;

public class test 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.button_example);
    registerForContextMenu(btn);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Context Menu");
    menu.add(0, v.getId(), 0, "Action 1");
    menu.add(0, v.getId(), 0, "Action 2");
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    if(item.getTitle()=="Action 1"){function1(item.getItemId());}
    else if(item.getTitle()=="Action 2"){function2(item.getItemId());}
    else {return false;}
return true;
}

public void function1(int id){
    Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();
}
public void function2(int id){
    Toast.makeText(this, "function 2 called", Toast.LENGTH_SHORT).show();
}
}   

我已经看到这个Question这很有用,因此我修改了我的代码,但它不起作用。我不知道出了什么问题。 这是经过修改的。

package com.a.c;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.Toast;

public class DemoActivity extends Activity{

    Button Buildings = (Button) findViewById(R.id.button1);   
    Button foodcourt = (Button) findViewById(R.id.button2);    

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


            registerForContextMenu(Buildings); 

            registerForContextMenu(foodcourt);   
        }   

    @Override  
       public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {   
        super.onCreateContextMenu(menu, v, menuInfo); 
        if (v == Buildings) { 
            menu.setHeaderTitle("Context Menu");   
            menu.add(0, v.getId(), 0, "Action 1");   
            menu.add(0, v.getId(), 0, "Action 2");  }

        else if (v == foodcourt) { 
                     menu.setHeaderTitle("Context Menu");   
                     menu.add(0, v.getId(), 0, "Action 1");   
                     menu.add(0, v.getId(), 0, "Action 2");  
                     menu.add(0, v.getId(), 0, "Action 5");
                     }    
       }   

       @Override  
        public boolean onContextItemSelected(MenuItem item) {
          if(item.getTitle()=="Action 1"){function1(item.getItemId());}   
          else if(item.getTitle()=="Action 2"){function2(item.getItemId());}   
        else if(item.getTitle()=="Action 3"){function3(item.getItemId());}
         else if(item.getTitle()=="Action 4"){function4(item.getItemId());}
          else if(item.getTitle()=="Action 5"){function5(item.getItemId());}

          else {return false;}   
         return true;   
         }   


        public void function1(int id){   
         Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();   
       }   
       public void function2(int id){   
          Toast.makeText(this, "function 2 called", Toast.LENGTH_SHORT).show();   
       }   

      public void function3(int id){   
        Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();   
      }   
      public void function4(int id){   
         Toast.makeText(this, "function 2 called", Toast.LENGTH_SHORT).show();   
      }
      public void function5(int id){   
          Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();   
        } 

  }  

1 个答案:

答案 0 :(得分:0)

onCreate()内进行按钮初始化,如下所示:

public class DemoActivity extends Activity{

    Button buildings;   
    Button foodcourt;    

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        buildings = (Button) findViewById(R.id.button1);
        foodcourt = (Button) findViewById(R.id.button2); 

        registerForContextMenu(Buildings); 
        registerForContextMenu(foodcourt);   
}
//rest of your code...