How to get text from EditText?(如何从 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() 中尝试它时,我得到空值.因此,为了获得最佳实践,您是否推荐一个单独的类来引用 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 获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 EditText 获取文本?
基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iOS4 创建后台定时器 2022-01-01
