2011-12-31 8 views
1

私はいくつかのボタンがあるウィンドウを持っています。私はコンテキストメニューを表示するためにボタンをクリックしたいが、他のものが押されていれば、別のコンテキストメニューがコンテキストメニューの他のコンテンツと共に表示される。 は、ここで私は参考になりましたし、それに応じて、私は私のコードを変更し、この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 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(); 
} 
} 

ですが、それは動作しません。私は何がうまくいかないのか分からない。 これは変更されたものです。

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(); 
     } 

    } 

答えて

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... 
+0

✿ – user1009889

関連する問題