通过安卓中<include>标签findViewById时出现的bug及解决方案
来源:互联网 发布:会计试题软件 编辑:程序博客网 时间:2024/06/06 04:37
http://blog.csdn.net/razor1991/article/details/9052217
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //第一种情况:间接得到文本组件(会得到两个)
- textView1=(TextView) findViewById(R.id.include1).findViewById(R.id.text);
- textView2=(TextView) findViewById(R.id.include2).findViewById(R.id.text);
- //第二种情况:直接得到文本组件
- textView3=(TextView) findViewById(R.id.text);
- //更改textView3的文字
- textView3.setText("啦啦啦啦啦啦啦啦啦");
- System.out.println("textView1="+textView1);
- System.out.println("textView2="+textView2);
- System.out.println("textView3="+textView3);
- }
这是my_include.xml文件,也就是需要被include的自定义布局文件
里面只是简简单单地摆了个TextView
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/linearLayout"
- android:orientation="vertical"
- android:gravity="center"
- >
- <TextView
- android:id="@+id/text"
- android:text="看到此控件者得永生..."
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
接下来是activity_main.xml文件
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <include
- android:id="@+id/include1"
- android:layout_width="wrap_content"
- layout="@layout/my_include" />
- <include
- android:id="@+id/include2"
- android:layout_width="wrap_content"
- layout="@layout/my_include" />
- </LinearLayout>
其中include了两次my_include.xml布局文件
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <include
- android:id="@+id/include1"
- android:layout_width="wrap_content"
- layout="@layout/my_include" />
- <include
- android:id="@+id/include2"
- android:layout_width="wrap_content"
- layout="@layout/my_include" />
- </LinearLayout>
最后是MainActivity.java文件,其中包含了主要代码
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //第一种情况:间接得到文本组件(会得到两个)
- textView1=(TextView) findViewById(R.id.include1).findViewById(R.id.text);
- textView2=(TextView) findViewById(R.id.include2).findViewById(R.id.text);
- //第二种情况:直接得到文本组件
- textView3=(TextView) findViewById(R.id.text);
- //更改textView3的文字
- textView3.setText("啦啦啦啦啦啦啦啦啦");
- System.out.println("textView1="+textView1);
- System.out.println("textView2="+textView2);
- System.out.println("textView3="+textView3);
- }
并附上打印信息和手机截图
从上图可以看出,通过直接findViewById的方式得到的对象默认为第一个找到的那个对象(这有点像html,虽然同一个页面可以有相同的ID,但是寻找ID时,默认只找到第一个ID就结束了).
因此无法批量操作具有相同ID的控件
结论:通过间接和直接的方式都可以获取include内部的组件,
但是,间接的方式可以获得两个TextView组件,而直接的方式只能获取到第一个TextView.
因此,在同一个xml文件中同时引用多个同一个layout文件时,笔者推荐大家使用间接findViewById的方式获取include内部的组件.
另外,这是笔者第一次写博客,排版很粗糙,还望各位多多包涵,若此文有错误的地方,还望各位指点,谢谢!
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
0 0
- 通过安卓中<include>标签findViewById时出现的bug及解决方案
- 通过安卓中<include>标签findViewById时出现的bug及解决方案
- [JQuery]关于使用jsp:include标签及<%@ include标签时要注意的事项
- 关于使用jsp:include标签及<%@ include标签时要注意的事项
- 深度了解scrollview嵌套listview会出现的bug及解决方案
- FragmentTabhost和viewPager一起使用会出现的小bug及解决方案
- Ubuntu14.04安装PIL(Image) Package出现的bug及解决方案
- XCODE 8 使用时遇到的bug及解决方案
- 如何通过include标签重用mybatis的代码段
- 如何通过include标签重用mybatis的代码段
- 如何通过include标签重用mybatis的代码段
- 通过include 加载的layout,在引用改layout的id时出现找不到控件
- include file="*.html" 时出现乱码解决方案
- 通过JAVA注解简化android的findViewById
- Android中include标签的使用及注意事项
- Android中include标签的使用及注意事项
- Android里merge和include标签的使用及区别
- Android中include标签的使用及注意事项
- 网桥(1)
- *仿惠锁屏侧滑锁屏的原理
- C++符号优先级
- OpenCV 矩形轮廓检测
- 面试题——数据库
- 通过安卓中<include>标签findViewById时出现的bug及解决方案
- 《深入理解Java虚拟机》读书笔记4——虚拟机类加载机制
- 验证手机号 和 邮箱格式
- 工业水处理:威立雅工业污水处理工艺基本要求
- java.io.IOException: Bad connect ack with firstBadLink as
- appfuse-Hibernate search 1
- OUYA——JDK的下载与安装(开发环境配置1)
- Android 项目中文件夹的作用
- 轻量级HTTP服务器Nginx(配置与调试Nginx)