1
我有一個帶有幾個按鈕的窗口。我希望點擊一個按鈕來顯示上下文菜單,但是如果按下另一個按鈕,則會在上下文菜單中顯示其他內容。 這裏是我的代碼將不同的上下文菜單分配給多個按鈕
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();
}
}
它的工作很多感謝✿ – user1009889 2011-12-31 10:07:36