我正在尝试在操作栏中创建自定义复制和粘贴菜单,但是当我在EditText区域中选择文本时,将弹出默认的复制和粘贴菜单,但是当我长按EditText区域而不是文本时,自定义菜单显示在操作栏中。如何在选择文本时显示自定义复制和粘贴菜单?
我看了这个类似的问题Override the general paste context menu in Android
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText Input = (EditText) findViewById(R.id.Input);
Input.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View p1)
{
// TODO: Implement this method
startActionMode(new ActionBarCallBack());
return false;
}
});
}
class ActionBarCallBack implements ActionMode.Callback
{
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item)
{
// TODO Auto-generated method stub
switch(item.getItemId()) {
case R.id.copyText:
Toast.makeText(getApplicationContext(), "Copy", Toast.LENGTH_SHORT).show();
return true;
case R.id.pasteText:
Toast.makeText(getApplicationContext(), "Paste", Toast.LENGTH_SHORT).show();
return true;
case R.id.cutText:
Toast.makeText(getApplicationContext(), "Cut", Toast.LENGTH_SHORT).show();
return true;
case R.id.help:
Toast.makeText(getApplicationContext(), "Help", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu)
{
// TODO Auto-generated method stub
mode.getMenuInflater().inflate(R.menu.contextual_menu, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode)
{
// TODO Auto-generated method stub
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu)
{
// TODO Auto-generated method stub
return false;
}
}
}