java Android 如何将字符串转换为可编辑的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13352531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android How can I Convert a String to a Editable
提问by Sam
We all know we can setText(String) to an editText box but if you want to get an editable variable from a string variable how do you do this?
我们都知道我们可以将 setText(String) 设置为 editText 框,但是如果您想从字符串变量中获取可编辑变量,您该怎么做?
For Temp I made an invisible editText box to set to then get from which turns the string into editable.
对于 Temp 我做了一个不可见的 editText 框来设置然后从中获取将字符串变成可编辑的。
re: for my own reference String again = editablevariable.getText().toString()
回复:再次供我自己参考 String = editablevariable.getText().toString()
回答by Sam
As you probably found out, Editable is an interface so you cannot call new Editable()
.
However an Editable is nothing more than a String with Spannables, so use SpannableStringBuilder:
您可能已经发现,Editable 是一个接口,因此您无法调用new Editable()
.
然而,一个 Editable 只不过是一个带有 Spannables 的 String,所以使用SpannableStringBuilder:
Editable editable = new SpannableStringBuilder("Pass a string here");
If you only want to change the Spannables, neverthe text itself, you can use a basic SpannableString.
如果您只想更改 Spannables 而不是文本本身,您可以使用基本的SpannableString。
回答by kyay
Use Editable.Factory.getInstance().newEditable(str)
From the android documentation:
使用Editable.Factory.getInstance().newEditable(str)
来自android 文档:
Returns a new SpannedStringBuilder from the specified CharSequence. You can override this to provide a different kind of Spanned.
从指定的 CharSequence 返回一个新的 SpannedStringBuilder。您可以覆盖它以提供不同类型的 Spanned。