如何从 EditText 获取文本?

时间:2023-05-04
本文介绍了如何从 EditText 获取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

这个问题很简单.但我想知道我们究竟在哪里引用了 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() 中尝试它时,我得到空值.因此,为了获得最佳实践,您是否推荐一个单独的类来引用 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 方法中.我认为这是个好主意
您的空值的原因可能是您尝试在 onCreate() 方法中设置 contentView() 之前调用 findViewById() -请检查一下.

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 获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:Android - 软键盘将我的活动布局推到屏幕外 下一篇:editText 没有失去焦点

相关文章

最新文章