简体   繁体   English

从TextView获取文本

[英]Get the text from a TextView

How can I get the text as String from a textview? 如何从textview获取文本字符串?

I tried: 我试过了:

myTextView.getText(); 

but it didn't work. 但它不起作用。

The correct way to get the text from a String is almost that! 从String获取文本的正确方法几乎就是这样! You simply forgot to use toString() 你只是忘了使用toString()

So, to get it as a string all you need to do is 因此,要将它作为字符串,您需要做的就是

myTextview.getText().toString();

this will give you the String you need (then just use it how you please). 这将为您提供所需的字符串(然后只需使用它)。

The reason just getText() did not work was because it returns an Editable object, not a String. getText()不起作用的原因是因为它返回一个Editable对象,而不是String。

From the documentation : 文档

This is the interface for text whose content and markup can be changed (as opposed to immutable text like Strings). 这是文本的接口,其内容和标记可以更改(与Strings等不可变文本相对)。 If you make a DynamicLayout of an Editable, the layout will be reflowed as the text is changed. 如果您创建可编辑的DynamicLayout,则会在文本更改时重新布局布局。

要从TextView获取文本,请使用.getText() ,然后使用.toString()将其转换为字符串

String s = myTextview.getText().toString();
String mString =myTextView.getText() + " ";

从getText()方法的Textview中获取Text,然后将其转换为String ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM