首页 > 解决方案 > 如果没有先单击复制,则禁止单击共享

问题描述

我的代码如下所示:

public boolean onOptionsItemSelected(MenuItem item){

    switch (item.getItemId()) {

        case R.id.share:

            // send copy text
            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBody = returnedText.getText().toString();
            String shareSub = "";
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSub);
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Share using"));
            Toast.makeText(getApplicationContext(),getString(R.string.send_text), Toast.LENGTH_SHORT).show();

            break;

    }

    switch (item.getItemId()) {

        case R.id.copy:

            // copy text to clipboard
            ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            clipboard.setText(returnedText.getText());
            Toast.makeText(getApplicationContext(),getString(R.string.copy_text), Toast.LENGTH_SHORT).show();

            break;

    }

    return super.onOptionsItemSelected(item);

}

我需要什么?如果他们没有先单击复制,则禁止单击共享。如果他们没有先点击复制,应用程序会通知 Toast 消息他们必须复制文本,然后,当他们点击复制时,要分享的菜单项具有自己的功能。

标签: javaandroid

解决方案


试试这个不确定它是否有效。

  1. 使 ClipboardManager 全局化。
  2. 如果是 R.id.share:

    if (clipboardManager.hasPrimaryClip()){
            // do what ever.
    }else {
            Toast.makeText(getApplicationContext(),"please click on copy first and try 
    again",Toast.LENGTH_LONG).show();
    }
    

推荐阅读