java - 如果没有先单击复制,则禁止单击共享
问题描述
我的代码如下所示:
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 消息他们必须复制文本,然后,当他们点击复制时,要分享的菜单项具有自己的功能。
解决方案
试试这个不确定它是否有效。
- 使 ClipboardManager 全局化。
如果是 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(); }
推荐阅读
- python - 为什么 set.intersection() 对 `list` 对象作为参数起作用?
- matplotlib - Matplotlib 生成的 PDF 中的绝对文本位置(用于文本的 figimage?)
- html - XPath:从最近的父节点中查找节点
- android - 为什么在 MaterialCardView 中设置 shapeAppearanceModel 会停止子剪辑?
- linux - 从 zip 文件中提取 200 行第一行并将此文件创建到不同的文件夹中
- git - 如何通过 git diff 命令仅获取已删除的行?
- c# - PDF 未下载 MVC dotnet core 3.1“无法下载”错误
- javascript - Navigator 中的“共享链接”在 Messenger 上不起作用
- c# - 在 docker 容器 C# App 之外连接被拒绝
- powershell - 带有 7zip 输出的 Powershell 调用命令