通过安卓中<include>标签findViewById时出现的bug及解决方案

来源:互联网 发布:会计试题软件 编辑:程序博客网 时间:2024/06/06 04:37

http://blog.csdn.net/razor1991/article/details/9052217



[java] view plaincopy
  1. protected void onCreate(Bundle savedInstanceState) {  
  2.         super.onCreate(savedInstanceState);  
  3.         setContentView(R.layout.activity_main);  
  4.           
  5.         //第一种情况:间接得到文本组件(会得到两个)  
  6.         textView1=(TextView) findViewById(R.id.include1).findViewById(R.id.text);  
  7.         textView2=(TextView) findViewById(R.id.include2).findViewById(R.id.text);  
  8.           
  9.         //第二种情况:直接得到文本组件  
  10.         textView3=(TextView) findViewById(R.id.text);  
  11.         //更改textView3的文字  
  12.         textView3.setText("啦啦啦啦啦啦啦啦啦");  
  13.           
  14.           
  15.         System.out.println("textView1="+textView1);  
  16.         System.out.println("textView2="+textView2);  
  17.         System.out.println("textView3="+textView3);  
  18.     }  

这是my_include.xml文件,也就是需要被include的自定义布局文件

里面只是简简单单地摆了个TextView

[java] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:id="@+id/linearLayout"  
  6.     android:orientation="vertical"   
  7.     android:gravity="center"  
  8.     >  
  9.     <TextView   
  10.         android:id="@+id/text"  
  11.         android:text="看到此控件者得永生..."  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         />  
  15. </LinearLayout>  

接下来是activity_main.xml文件

[java] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:gravity="center"  
  6.     android:orientation="vertical"  
  7.     tools:context=".MainActivity" >  
  8.   
  9.     <include  
  10.         android:id="@+id/include1"  
  11.         android:layout_width="wrap_content"  
  12.         layout="@layout/my_include" />  
  13.   
  14.     <include  
  15.         android:id="@+id/include2"  
  16.         android:layout_width="wrap_content"  
  17.         layout="@layout/my_include" />  
  18.   
  19. </LinearLayout>  

其中include了两次my_include.xml布局文件

[java] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:gravity="center"  
  6.     android:orientation="vertical"  
  7.     tools:context=".MainActivity" >  
  8.   
  9.     <include  
  10.         android:id="@+id/include1"  
  11.         android:layout_width="wrap_content"  
  12.         layout="@layout/my_include" />  
  13.   
  14.     <include  
  15.         android:id="@+id/include2"  
  16.         android:layout_width="wrap_content"  
  17.         layout="@layout/my_include" />  
  18.   
  19. </LinearLayout>  

 最后是MainActivity.java文件,其中包含了主要代码

[java] view plaincopy
  1. protected void onCreate(Bundle savedInstanceState) {  
  2.         super.onCreate(savedInstanceState);  
  3.         setContentView(R.layout.activity_main);  
  4.           
  5.         //第一种情况:间接得到文本组件(会得到两个)  
  6.         textView1=(TextView) findViewById(R.id.include1).findViewById(R.id.text);  
  7.         textView2=(TextView) findViewById(R.id.include2).findViewById(R.id.text);  
  8.           
  9.         //第二种情况:直接得到文本组件  
  10.         textView3=(TextView) findViewById(R.id.text);  
  11.         //更改textView3的文字  
  12.         textView3.setText("啦啦啦啦啦啦啦啦啦");  
  13.           
  14.           
  15.         System.out.println("textView1="+textView1);  
  16.         System.out.println("textView2="+textView2);  
  17.         System.out.println("textView3="+textView3);  
  18.     }  


并附上打印信息和手机截图

从上图可以看出,通过直接findViewById的方式得到的对象默认为第一个找到的那个对象(这有点像html,虽然同一个页面可以有相同的ID,但是寻找ID时,默认只找到第一个ID就结束了).

因此无法批量操作具有相同ID的控件

 

结论:通过间接和直接的方式都可以获取include内部的组件,

但是,间接的方式可以获得两个TextView组件,而直接的方式只能获取到第一个TextView.

 

因此,在同一个xml文件中同时引用多个同一个layout文件时,笔者推荐大家使用间接findViewById的方式获取include内部的组件.

 

另外,这是笔者第一次写博客,排版很粗糙,还望各位多多包涵,若此文有错误的地方,还望各位指点,谢谢!




[java] view plaincopy
  1. <pre></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre>  
  7. <pre></pre>  
  8. <pre></pre>  
  9. <pre></pre>  
  10. <pre></pre>  

0 0