如何从 Android 小部件中的文本框检索文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3606646/
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
How to Retrieve Text from Textbox in Android widget?
提问by Pow
I'm at the very early stage of learning Android. Can anyone tell me how I can use the text written in a Text box by user?
我正处于学习 Android 的早期阶段。谁能告诉我如何使用用户在文本框中编写的文本?
I'm trying to use getText() method but finding it confusing.
我正在尝试使用 getText() 方法,但发现它令人困惑。
Thanks in advance.
提前致谢。
回答by Michel
getText should do it.
getText 应该这样做。
you maybe need a string cast?
你可能需要一个字符串转换?
what is the code you using @ the moment?
你现在使用@的代码是什么?
example:
例子:
EditText editText = (EditText)findViewById(R.id.yourId);
String editTextStr = editText.getText().toString()
docs:
文档:
http://developer.android.com/reference/android/widget/TextView.html#getText(
http://developer.android.com/reference/android/widget/TextView.html#getText(
回答by Banshi
TextBox t = (TextBox)findViewById(R.id.textboxid)
String s = "";
s = t.getText().toString();
Here you can get content of textbox
在这里你可以得到文本框的内容
回答by Falmarri
I think what might be confusing him is that getText()
doesn't return a string. You have to do getText().toString()
我认为可能让他感到困惑的是它getText()
不返回字符串。你必须要做getText().toString()
回答by mhshams
i think you are looking for TextView
我想你正在寻找 TextView
http://developer.android.com/reference/android/widget/EditText.html
http://developer.android.com/reference/android/widget/EditText.html
getText
and setText
methods are avaialbe in this class to set and get text value.
getText
和setText
方法可以在这个类中设置和获取文本值。
回答by DeRagan
yes you need to retrieve the text by using getText() method. What is it that you find confusing here?
是的,您需要使用 getText() 方法检索文本。你在这里感到困惑的是什么?
In short you need to say Textview.getText()
总之你需要说 Textview.getText()