问题
I created a new menu, named drmenu.xml. It works correctly when i press menu button, but I need to open a context menu when the user press the button. The below code the button just show a toast.
this is my xml layout:
<LinearLayout
android:id="@+id/MenuCall"
android:layout_width="90dip"
android:layout_height="match_parent"
android:gravity="right|center_vertical" >
<ImageView
android:id="@+id/MenuCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/imageiew6" />
</LinearLayout>
and this is my java code:
final LinearLayout callback_var = (LinearLayout) findViewById(R.id.MenuCall);
registerForContextMenu(callback_var);
callback_var.setOnClickListener(new android.view.View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "this is repeated", Toast.LENGTH_LONG).show();
openContextMenu(callback_var);
}
回答1:
If you want create a context Menu, you have to call the method registerForContextMenu()
passing it the View that should be associated with the context menu.
For example, supposing to associate the context menu with a Button:
Button button = (Button) findViewById(R.id.my_button);
registerForContextMenu(button);
which can be called in the onCreate() of your Activity. Then, inside the same activity, you need to override the onCreateContextMenu()
method.
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_context_menu, menu);
}
Then you have to implement onContextItemSelected()
, for triggering the proper action when a item inside the context menu is pressed:
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.first_action:
// your first action code
return true;
case R.id.second_action:
// your second action code
return true;
default:
return super.onContextItemSelected(item);
}
}
Now the long press click on the button opens the context menu you defined in the your_context_menu.xml
file.
Consider that opening the context menu with a long press is compliant with the Android standard UI, however If you want your context menu to appear on a simple tap you can look here the answer
NOTE: As said here
An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).
回答2:
as Joseph82 said for context menu we can use that way . and i find for use option menu we i can use blow code : in Oncreate Method write this :
final LinearLayout callback_var = (LinearLayout) findViewById(R.id.MenuCall);
callback_var.setOnClickListener(new android.view.View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
openOptionsMenu();
}
});
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater mnu = getMenuInflater();
mnu.inflate(R.menu.darunamamenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId())
{
case R.id.firstpage:
break;
case R.id.exit:
finish();
System.exit(0);
break;
case R.id.setting:
Intent Settingact = new Intent(G.currentActivity, SettingActivity.class);
G.currentActivity.startActivity(Settingact);
G.currentActivity.finish();
break;
case R.id.regalarm:
Intent RegAlarm = new Intent(G.currentActivity, alarmsetter.class);
G.currentActivity.startActivity(RegAlarm);
G.currentActivity.finish();
break;
case R.id.inform:
Intent inform = new Intent(G.currentActivity, ActivityInformation.class);
G.currentActivity.startActivity(inform);
G.currentActivity.finish();
break;
}
return super.onOptionsItemSelected(item);
}
来源:https://stackoverflow.com/questions/26146818/opening-a-floating-menu-context-menu-in-android