如果未先单击“复制”,则禁止单击“共享”

时间:2019-03-30 04:26:40

标签: java android

我的代码如下:

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消息,他们必须复制文本,然后,当他们单击“复制”时,“共享”菜单项具有其自己的功能。

1 个答案:

答案 0 :(得分:0)

不确定是否可以尝试。

  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();
    }