【发布时间】:2011-12-21 03:12:05
【问题描述】:
目前我小心翼翼地为每个小部件使用不同的 ID,但是如果我对不同布局 xml 文件中的某些小部件使用相同的 ID,那么我对 findViewById 的调用会发生什么情况?它会感到困惑并从错误的布局文件中返回一个小部件吗?
我问是因为我正在考虑在其他一些视图中包含一个视图(带有一排按钮),然后每个屏幕类都必须为包含的视图上的每个按钮分配一个稍微不同的行为。但要做到这一点,他们会在同一个 ID 上调用 findViewById。 例如,目前我在每个文件中都这样做:
final ImageButton homeButton = (ImageButton)this.findViewById(R.id.rshome_button);
homeButton.setOnClickListener(new BottomButtonClickListener());
final ImageButton hotButton = (ImageButton)this.findViewById(R.id.rshotlist_button);
hotButton.setOnClickListener(new BottomButtonClickListener());
final ImageButton locButton = (ImageButton)this.findViewById(R.id.rsbot_map);
locButton.setOnClickListener(new BottomButtonClickListener());
final ImageButton sendButton = (ImageButton)this.findViewById(R.id.rssend_button);
sendButton.setOnClickListener(new BottomButtonClickListener());
但我将每个文件中的 R.id 更改为指向每个关联布局中的小部件。如果我可以使用一个 R.id 就好了,这样我就不必调整每个布局文件和每个屏幕类。
谢谢
【问题讨论】: