android.app.Activity中
publicViewfindViewById(intid)
Since:APILevel1
FindsaviewthatwasidentifiedbytheidattributefromtheXMLthatwasprocessedinonCreate(Bundle).
Returns
*Theviewiffoundornullotherwise.
这里的findViewById是在当前Active的View根及其儿子中通过Id寻找View
Active的View根一般是在其在onCreate函数中可以通过setContentView(R.layout.main)这种形式来设置。
android.view.View中
publicfinalViewfindViewById(intid)
Since:APILevel1
Lookforachildviewwiththegivenid.Ifthisviewhasthegivenid,returnthisview.
Parameters
idTheidtosearchfor.
Returns
*Theviewthathasthegivenidinthehierarchyornull
因为是findViewById在是从View及其儿子中查找,所以即使几个layout的XML文件中的View的id号相同的话,只要他们没有相同的父节点,或有相同的父亲节点,但不在父节点及以上节点调用findViewById通过id来查找他们就是没有问题。
一般我都是在layout的XML文件中设置View的id。
比如:
<TextView
android:id="@+id/lable2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:editable="false"/>
<Viewandroid:id="@+id/View01"android:layout_width="wrap_content"android:layout_height="wrap_content"></View>
分享到:
相关推荐
一个简单的注解实现 findViewById 和 setOnClickListener 的例子
这是可以让你告别繁杂的findViewById的一个实例,通过注解的方式来实现。
用DOM4J 写的一个小工具类 避免麻烦的FindViewById的书写
Android注解省去findviewbyid,在Android开发的过程中有很多控件都要findviewbyid的,这样太麻烦了。
简单的注解类,代替android中的findViewById().
Android开发中经常使用 findviewbyid来找控件, 这个工具类可以方便的生成代码,减少代码的书写。
一个根据布局文件自动生成findViewById代码的android studio插件
支持链式操作 vq id R id TextView1 text "I find hello world" ; vq id R id button1 text "find it " clicked this "onClickMethod" ;...vq id R id imageView1 image android R ...
自动生成FindViewById工具
简单实现Android的findviewbyid IOC框架
需要导入jar包该工具类直接extends viewutils ,所有的findviewbyid都使用$来表示!
Android技术知识点:如何使用视图绑定来消除findViewById()
自定义注解,实现了findViewById 和 OnCLIckListener 注解不会去新建一个OnClickListener对象,所有view都指向一个listener, 然后通过反射调用方法去执行
自动生成findviewbyid插件演示demo
AndroidFindView FindViewById无需转换类型进口 将其添加到项目库的末尾的build.gradle中: repositories { // ... maven { url " https://jitpack.io " }} 步骤2.在表单中添加依赖项 dependencies { compile ' ...
快速生成findviewbyid的插件.zip,A plugin for android developer, with the plugin you can generate "findViewById" code quickly.
为了减少频繁的调用findViewById(R.id...),可以采用一些注入框架,可以简化自己的代码,让你更专注于实际的功能开发,butterknife就是这样的一个框架.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,...
LinearLayout groupPollingAddress = (LinearLayout)findViewById(R.layout.fragment_field_list); 返回为null。 【解决过程】 1.参考: android – getActivity().findViewById(R.layout.contacts_list_view) ...
主要介绍了AndroidGUI27中findViewById返回null的快速解决办法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
从 Android Studio 3.6 开始,视图绑定能够通过生成绑定对象来替代 findViewById,从而可以帮您简化代码、移除 bug,并且从 findViewById 的模版代码中解脱出来,今天通过本文给大家介绍使用视图绑定替代 ...