不推荐使用Android Clipboard.getText();然后如何获取其中的文本项? [英] Android clipboard.getText() is deprecated; how then to get the text item that's in it?

查看:155
本文介绍了不推荐使用Android Clipboard.getText();然后如何获取其中的文本项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很好用,向我确切地显示了放入剪贴板中的最后一个字符串是什么,恰好是 euswcnmst



Log.w( clip,剪贴板.getText()。toString());



但是对于剪贴板对象,不推荐使用 getText



同时,如果我执行 Log.w( clip,剪贴板.getPrimaryClip()。toString()); ,我得到的结果与
ClipData {text / plain label {T:euswcnmst}}



我明白了我想要的,并假设此格式始终用于字符串剪贴板项目,我可以使用 String 函数(查找和后续的} 并执行 substring )来提取 euswcnmst ,但这是一个黑客。 / p>

我应该做什么?



编辑



根据Commonsware的回答,这是我应该做的事情:

  ClipData剪辑=剪贴板。 getPrimaryClip(); 

if(clip == null || clip.getItemCount()== 0)
return; // ... 随你;只是不要转到下一行

字符串t = clip.getItemAt(0).getText()。toString();

EDIT 2



如果剪贴板中的最后一个ITEM不是文本,则上面的代码将给出此错误:



java.lang.NullPointerException:尝试在空对象引用上调用接口方法'java.lang.String java.lang.CharSequence.toString()'



我在下面添加了第三行):

  if(clip == null 
|| clip.getItemCount()= = 0
|| clip.getItemCount()> 0&& clip.getItemAt(0).getText()== null

return; // ... 随你;只是不要转到下一行


解决方案

请理解剪贴板不仅仅用于文本。复杂的构造可以以 ClipData 对象内1到N ClipData.Item 对象的形式放置在剪贴板上您从 getPrimaryClip()获取。



给出您的 ClipData ,调用 getItemCount()确定项目数。对于您希望尝试使用的任何项目,请在 ClipData 上调用 getItemAt() ClipData.Item 。在该项目上,您可以调用 getText() coerceToText()尝试获取该项目的文本表示形式


This works fine, showing me exactly what was the last string put into Android clipboard, which happens to be euswcnmst.

Log.w("clip", clipboard.getText().toString());

But getText is deprecated for clipboard objects.

Meanwhile, if I do Log.w("clip", clipboard.getPrimaryClip().toString());, I get this, exactly as shown ClipData { text/plain "label" {T:euswcnmst} }

I see what I want, and, assuming this format is always used for string clipboard items, I can use String functions (find : and subsequent } and do substring) to extract euswcnmst, but that's a hack.

What SHOULD I do?

EDIT

Based on Commonsware's Answer, here's what I should do:

ClipData clip = clipboard.getPrimaryClip();

if(clip == null || clip.getItemCount() == 0)
  return; // ... whatever; just don't go to next line

String t = clip.getItemAt(0).getText().toString();

EDIT 2

If the last ITEM in the clipboard was NOT text, the above code gives this error:

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String java.lang.CharSequence.toString()' on a null object reference

Here's the fix (I added the third line below):

if(   clip == null 
   || clip.getItemCount() == 0 
   || clip.getItemCount() > 0 && clip.getItemAt(0).getText() == null
  )
    return; // ... whatever; just don't go to next line

解决方案

Please understand that the clipboard is not purely for text. Complex constructs can be placed on the clipboard, in the form of 1 to N ClipData.Item objects inside the ClipData object that you are getting from getPrimaryClip().

Given your ClipData, call getItemCount() to determine the number of items. For whichever item(s) you wish to attempt to use, call getItemAt() on the ClipData to get the corresponding ClipData.Item. On that item, you can call getText() or coerceToText() to try to get a text representation of that item.

这篇关于不推荐使用Android Clipboard.getText();然后如何获取其中的文本项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆