选择文本后出现自定义复制和粘贴菜单 [英] making custom copy and paste menu appear when text is selected

查看:78
本文介绍了选择文本后出现自定义复制和粘贴菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在操作栏中创建一个自定义的复制和粘贴菜单,但是当我在EditText区域中选择文本时,将弹出默认的复制和粘贴菜单,但是当我长按EditText区域而不是文本时,自定义菜单将显示在操作"栏中.选择文本后,如何显示我的自定义复制和粘贴菜单?

I am trying to create a custom copy and paste menu in the Action bar but when I select the text in the EditText area the default copy and paste menu will pop up, but when I long press the EditText area instead of the text, the custom menu appears in the Action bar. How can I have my custom copy and paste menu appear when the text is selected?

我看过这个类似的问题在Android

I looked at this similar question 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;
    }
}

}

推荐答案

尝试使用

Try Using setCustomSelectionActionModeCallback() instead of setOnLongClickListener()

这篇关于选择文本后出现自定义复制和粘贴菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆