Android-Text and Input

文字管理器

复制粘贴

复制粘贴的对象分如下3中:text文本,URI, intent

那么剪切板到底提供了哪些api呢 

ClipboardManager  这个类一看名字就知道是老家伙了 获取方式getSystemService(CLIPBOARD_SERVICE).

ClipData, ClipData.Item, and ClipDescription   把数据封装到这个类中

clipData有什么方法呢?
newPlainText(label, text) 返回的是一个ClipData.Item包含了一个字符串的类 其中label就是ClipDescription
newIntent(label, intent)  同时同理的

下面介绍怎么强制将剪切板的内容转换成文本  用这个方法ClipData.Item.coerceToText()

下面演示怎么

Copying to the Clipboard 复制内容得到剪切板


1.如果你copy的数据是一个uri 先确定你初始化了一个content provider
2.得到系统的剪切板

...

// if the user selects copy
case R.id.menu_copy:

// Gets a handle to the clipboard service.
ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);
3.将数据复制到clipData上面
For a text
// Creates a new text clip to put on the clipboard
ClipData clip = ClipData.newPlainText("simple text","Hello, World!");

For a URI 
// Creates a Uri based on a base Uri and a record 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 URIs that you use to copy data
private static final String COPY_PATH = "/copy";

// Declares the Uri to paste to the clipboard
Uri copyUri = Uri.parse(CONTACTS + COPY_PATH + "/" + lastName);

...

// Creates a new URI clip object. The system uses the anonymous getContentResolver() object to
// get MIME types from provider. The clip object's label is "URI", and its data is
// the Uri previously created.
ClipData clip = ClipData.newUri(getContentResolver(),"URI",copyUri);
For an intent
// Creates the Intent
Intent appIntent = new Intent(this, com.example.demo.myapplication.class);

...

// Creates a clip object with the Intent in it. Its label is "Intent" and its data is
// the Intent object created previously
ClipData clip = ClipData.newIntent("Intent",appIntent);

4.将数据放到剪切板上面去
// Set the clipboard's primary clip.
clipboard.setPrimaryClip(clip);


然后就是如何复制的了

Pasting from the Clipboard 从剪切板里面复制内容


1.粘贴文本

ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

String pasteData = "";
2.判断应用程序是否禁用了粘贴功能
3.粘贴内容
// Responds to the user selecting "paste"
case R.id.menu_paste:

// Examines the item on the clipboard. If getText() does not return null, the clip item contains the
// text. Assumes that this application can only handle one item at a time.
 ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);

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

// If the string contains data, then the paste operation is done
if (pasteData != null) {
    return;

// The clipboard does not contain text. If it contains a URI, attempts to get data from it
} else {
    Uri pasteUri = item.getUri();

    // If the URI contains something, try to get text from it
    if (pasteUri != null) {

        // calls a routine to resolve the URI and get data from it. This routine is not
        // presented here.
        pasteData = resolveUri(Uri);
        return;
    } else {

    // Something is wrong. The MIME type was plain text, but the clipboard does not contain either
    // text or a Uri. Report an error.
    Log.e("Clipboard contains an invalid data type");
    return;
    }
}
从intent里面粘贴内容
// Gets a handle to the Clipboard Manager
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

// 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) {

    // handle the Intent

} else {

    // ignore the clipboard, or issue an error if your application was expecting an Intent to be
    // on the clipboard
}

总体来说 Android官方给出了3个板块来讲这部分
第一部分是 复制粘贴 这个功能是最基本的 平常要是用 最多就是用这个
第二部分 就是手机输入法编辑器 就是可以让我们定制自己的手机输入法 比如现在大家都在用的搜狗输入法就是按照这些api完成的
第三部分 是拼写检查 然而对于我们中国人来说并没有什么用 因为我们是象形文字












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值