I am using the following method to add a ContextMenu
to a custom view i have built but i want to know how to react to the press of that contextmenu.
This is not an Activity so i cannot do this:
@override
public boolean onOptionsItemSelected(MenuItem item) {
Here is the code
private View.OnCreateContextMenuListener vC = new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu arg0, View arg1,
ContextMenuInfo arg2) {
// TODO Auto-generated method stub
arg0.add(0, 0, 0, "Call");
arg0.add(0, 1, 0, "Map");
arg0.add(0, 2, 0, "Market");
}
};
Update:
Here is a very simplified verion of my class.
public final class NewView extends View {
public NewView(Context context, AttributeSet attrs) {
super(context, attrs);
cntxt = context;
this.setLongClickable(true);
this.setO开发者_JS百科nLongClickListener(vLong);
this.setOnCreateContextMenuListener(vC);
}
private View.OnLongClickListener vLong = new View.OnLongClickListener() {
public boolean onLongClick(View view) {
showContextMenu();
return true;
}
};
private View.OnCreateContextMenuListener vC = new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu arg0, View arg1,
ContextMenuInfo arg2) {
// TODO Auto-generated method stub
arg0.add(0, 0, 0, "Call");
arg0.add(0, 1, 0, "Map");
arg0.add(0, 2, 0, "Market");
}
};
}
Noone seem to answer exhaustively TS' question (and mine eventually), thus let me post a solution to this. Given the code above you can attach custom MenuItem's click handlers:
private View.OnCreateContextMenuListener vC = new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(0, 0, 0, "Call")
.setOnMenuItemClickListener(mMenuItemClickListener);
menu.add(0, 1, 0, "Map")
.setOnMenuItemClickListener(mMenuItemClickListener);
menu.add(0, 2, 0, "Market")
.setOnMenuItemClickListener(mMenuItemClickListener);
}
};
private OnMenuItemClickListener mMenuItemClickListener = new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case 0:
// do "Call"
return true;
case 1:
// do "Map"
return true;
case 2:
// do "Market"
return true;
}
return false;
}
};
};
Use item.getItemId()
and create switch
and cases
based on the number returned by getItemId()
Something like this.
@override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case 1:
Log.i("FIRST ITEM: ", "CALL");
break;
case 2:
Log.i("2nd ITEM: ", "MAP");
break;
case 3:
Log.i("3rd ITEM: ", "Market");
break;
default:
}
}
I hope this is what you meant by reacting on menu items selection. :)
This does seem to be an odd inconsistency in Android - a View can create a context menu, but the handling of said menu can only happen in completely different code?
I'm also solving this with setOnMenuItemClickListener(). The documentation suggests this is reasonable, but it requires effort to set it up if you are using a MenuInflater.
public void onCreateContextMenu(final ContextMenu menu, final View v,
final ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
final MenuInflater menuInflater = this.menuInflater;
menuInflater.inflate(R.menu.context_date, menu);
final int length = menu.size();
for (int index = 0; index < length; index++) {
final MenuItem menuItem = menu.getItem(index);
menuItem.setOnMenuItemClickListener(this);
}
}
You could assign a variable to your View
from your activity after you inflate the main layout (in `onCreate).
myView = (View) findViewById(R.id.my_view);
Next, do registerForContextMenu(myView);
Finally, you can override onCreateContextMenu
where you can add what happens when the context menu appears.
In the view :
Let your view implements ContextMenuInfo
public class MyView extends View implements ContextMenuInfo
Then, override getContextMenuInfo to return your view.
@Override
protected ContextMenuInfo getContextMenuInfo() {
return this ;
}
In you activity :
Register your view for ContextMenu.
registerForContextMenu(myView);
Then access the view in your activity onContextItemSelected.
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
...
case R.id.action_ctx_wordview_read_tts:
MyView myView = (MyView) item.getMenuInfo();
...
}
}
Note :
When I add an onClickListener to the MyView, the contextMenu stoped working. I had to add the following code to solve the problem.
tvWord.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return false;
}
});
精彩评论