android 使自定义复制和粘贴菜单出现时,文本是选定的

x6yk4ghg  于 2023-05-21  发布在  Android
关注(0)|答案(2)|浏览(130)

我试图在操作栏中创建一个自定义复制和粘贴菜单,但当我选择编辑文本区域中的文本时,默认的复制和粘贴菜单将弹出,但当我长按编辑文本区域而不是文本时,自定义菜单出现在操作栏中。当文本被选中时,如何让我的自定义复制和粘贴菜单出现?
我看了这个类似的问题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;
    }
}

}
mefy6pfw

mefy6pfw1#

尝试使用setCustomSelectionActionModeCallback()而不是setOnLongClickListener()

myzjeezk

myzjeezk2#

完整的样品波纹管。在我的例子中,它是添加一个“使用相机扫描”上下文菜单。请注意,此解决方案不会覆盖EditText中选定文本的上下文菜单:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    cbEditText.setCustomInsertionActionModeCallback(new ActionMode.Callback() {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            menu.add(0, 1, 0, R.string.edt_scan_camera);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            if (1 == item.getItemId()) {
                //todo: perform your action here!
                return true;
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    });
}

相关问题