我做了一个上下文动作模式,当我单击自己创建的选项时,它运行得很好,但问题是动作模式只能运行一次并再次启用,我已经退出了应用程序并再次运行
这是我的代码
Button.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (actionMode != null) {
return false;
}
actionMode = startActionMode(startActionMode);
return true;
}
});
}
private ActionMode.Callback startActionMode = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.menu12, menu);
mode.setTitle("choose your option");
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(MainActivity.this, "item 1 pressed", Toast.LENGTH_SHORT).show();
mode.finish();
return true;
case R.id.item2:
Toast.makeText(MainActivity.this, "item 2 pressed", Toast.LENGTH_SHORT).show();
mode.finish();
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
startActionMode = null;
}
};
}
简单代替