如何从 EditText 获取文本?

How to get text from EditText?(如何从 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 获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)