如何从EditText上获得的文本? [英] How to get text from EditText?
问题描述
现在的问题是很简单的。 但我想知道我们究竟让我们引用的GUI元素? 正如这是确定的最佳地点:
The question is quite simple. But I want to know where exactly do we make our references to the gui elements? As in which is the best place to define:
final EditText edit = (EditText) findViewById(R.id.text_xyz);
edit.getText.tostring();
当我尝试做了默认的OnCreate()内我得到空值。 因此,最佳实践,做u建议一个单独的类来引用main.xml中这些已经定义GUI元素。从这里我们可以称之为像gettext的或的setText这些元素的各种方法?
When I try it doing inside the default oncreate() I get null values. So for best practice, do u recommend a separate class for referring these already defined gui elements in main.xml. From here we can call various methods of these elements like gettext or settext?
推荐答案
嗯,这取决于你的需要。很多时候,我把我提到的小部件的活动(为一类字段) - 并将它们在的onCreate
方法。我认为这是一个好主意
也许你空的原因是,你试图调用 findViewById()
设置之前内容查看()
在你的的onCreate()
方法 - 请检查
Well, it depends on your needs. Very often I keep my references to widgets in activity (as a class fields) - and set them in onCreate
method. I think that is a good idea
Probably the reason for your nulls is that you are trying to call findViewById()
before you set contentView()
in your onCreate()
method - please check that.
这篇关于如何从EditText上获得的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!