如何在弹出窗口的edittext中启用默认的复制/粘贴选项?

时间:2019-05-23 13:28:50

标签: android

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/llMain"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="400dp"
    android:layout_height="300dp"
    android:orientation="vertical"
    android:background="@color/colorAccent"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter here"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我相信您正在寻找以编程方式粘贴的内容。

ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
String pasteData = "";

 // If it does contain data, decide if you can handle the data.
if (!(clipboard.hasPrimaryClip())) {

} else if (!(clipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN))) {

    // since the clipboard has data but it is not plain text

} else {

    //since the clipboard contains plain text.
    ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);

    // Gets the clipboard as text.
    pasteData = item.getText().toString(); 
}