我想在Android API上测试剪贴板,所以我开始使用Android的ClipboardManager和ClipData类创建一个简单的应用程序。我跟随Android网站上的Copy and Paste guide,虽然我不得不填写几个点,因为本指南没有必要的每行代码(我最终不得不注释掉Intent和URI粘贴方法,因为它没有'给出具体的例子,因为我假设的是数据的多样性,但是)。我运行应用程序,但由于某种原因,我发送的文本似乎不起作用或显示。有谁知道这可能导致什么?
这是我的班级:
public class MainActivity extends Activity
{
// Creates a URI based on a base URI and a crecord ID based on the contact's last name
// Declares the base URI string
private static final String CONTACTS = "content://com.example.contacts";
// Declares a path string for the URIs that you use to copy data
private static final String COPY_PATH = "/copy";
// Declares the URI to paste to the clipboard
Uri copyURI;
// Declares a MIME type constant to match against the MIME types offered by the provider
private static final String MIME_TYPE_CONTACT = "vnd.android.cursor.item/vnd.example.contact";
private int pasteType;
private Menu menu;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String lastName = "Nichols";
copyURI = Uri.parse(CONTACTS + COPY_PATH + "/" + lastName);
setContentView(R.layout.activity_main);
main();
}
void main()
{
String text = "Hi, bob!";
copyText(text);
String data = paste();
TextView tv = new TextView(this);
tv.setText(data);
setContentView(tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
this.menu = menu;
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@TargetApi(11)
void copyIntent()
{
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
Intent appIntent = new Intent(this, com.example.clipboard.MainActivity.class);
ClipData clip = ClipData.newIntent("Intent", appIntent);
clipboard.setPrimaryClip(clip);
pasteType = 1;
}
@TargetApi(11)
void copyText(String text)
{
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("simple", text);
clipboard.setPrimaryClip(clip);
pasteType = 2;
}
@TargetApi(11)
void copyURI()
{
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newUri(getContentResolver(), "URI", copyURI);
clipboard.setPrimaryClip(clip);
pasteType = 3;
}
@TargetApi(11)
String paste()
{
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
/*if (pasteType == 1)
{
// Checks to see if the clip item contains an Intent, by testing to see if getIntent() returns null
Intent pasteIntent = clipboard.getPrimaryClip().getItemAt(0).getIntent();
if (pasteIntent != null)
{
}
}*/
if (pasteType == 2)
{
String pasteData = "";
// Gets the ID of the "paste" menu item
MenuItem mPasteItem = this.menu.findItem(R.id.paste);
// If the clipboard doesn't conatin data, disable the paste menu item
if (!(clipboard.hasPrimaryClip()))
mPasteItem.setEnabled(false);
else if (!(clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)))
{
// This disables the paste menu item, since the clipboard has data, but it is not plain text
mPasteItem.setEnabled(false);
}
else
mPasteItem.setEnabled(true);
// Examines the item on the clipboard. If getText() does not return null, the clip item contains the
// text. Assume the application can only handle one item at a time.
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
pasteData = (String) item.getText();
return pasteData;
}
/*if (pasteType == 3)
{
ContentResolver cr = getContentResolver();
ClipData clip = clipboard.getPrimaryClip();
if (clip != null)
{
ClipData.Item item = clip.getItemAt(0);
Uri pasteUri = item.getUri();
// If the clipboard contains a URI reference
if (pasteUri != null)
{
// Is this a content URI?
String uriMimeType = cr.getType(pasteUri);
// If the return value is not null, the Uri is a content URI
if (uriMimeType != null)
{
// Does the content provider offer a MIME type that the current application can use?
if (uriMimeType.equals(MIME_TYPE_CONTACT))
{
Cursor pasteCursor = cr.query(pasteUri, null, null, null, null);
if (pasteCursor != null)
{
if (pasteCursor.moveToFirst()) {
}
}
pasteCursor.close();
}
}
}
}
}*/
return null;
}
}
编辑:这是我的menu.xml文件,以帮助弄清楚我的菜单没有显示复制/粘贴选项的原因。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
答案 0 :(得分:0)
我认为您所关注的指南是一个完全误导!它缺乏如此多的信息。但是,我建议您简化代码:
Here is a good answer可能会启发你。
顺便说一句,就我所知,你的copy()
和paste()
方法完全没问题。您的问题与ContextMenu
以及您如何处理它有关。