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

这是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文件

其中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);
	}

 并附上打印信息和手机截图
 从上图可以看出,textView1与textView3其实是得到的同一个对象


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

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

 

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

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

 

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

 

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

 


 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
List<NaviLatLng> 是一个储存导航点坐标的集合,如果你想在安卓给这个集合添加点击事件,可以使用 ListView 控件和适配器来实现。首先,你需要创建一个适配器来将 List<NaviLatLng> 的元素显示在 ListView 。下面是一个简单的适配器示例: ``` public class NaviLatLngAdapter extends ArrayAdapter<NaviLatLng> { public NaviLatLngAdapter(Context context, List<NaviLatLng> list) { super(context, 0, list); } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, parent, false); } NaviLatLng naviLatLng = getItem(position); TextView textView = convertView.findViewById(android.R.id.text1); textView.setText(naviLatLng.toString()); return convertView; } } ``` 在这个适配器,我们使用了系统自带的 simple_list_item_1 布局来显示导航点坐标。接下来,在你的 Activity ,你可以使用 ListView 控件来显示 List<NaviLatLng> 的元素,并设置点击事件监听器。下面是一个示例代码: ``` ListView listView = findViewById(R.id.listView); NaviLatLngAdapter adapter = new NaviLatLngAdapter(this, wayList); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // 处理点击事件 NaviLatLng naviLatLng = wayList.get(position); Toast.makeText(MainActivity.this, "你点击了导航点:" + naviLatLng.toString(), Toast.LENGTH_SHORT).show(); } }); ``` 在这个示例,我们创建了一个 ListView 控件,并使用适配器将 List<NaviLatLng> 的元素显示在 ListView 。然后,我们设置了一个点击事件监听器,在用户点击某个导航点会弹出一个 Toast 提示用户点击了哪个导航点。你可以根据自己的需求来修改这个监听器的实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值