如何知道我在android中的文件main.xml中拥有的所有ID?
题
我是Android World的新手,我有疑问,是否有任何方法可以给我我在main.xml中创建的ID的名称?例如,我有这个:
main.xml
<TextView android:id="@+id/text1"
android:layout_width="70px"
android:layout_height="70px"
android:text="Google"
/>
<TextView android:id="@+id/text2"
android:layout_width="70px"
android:layout_height="70px"
android:text="As"
/>
我想要的是两个TextView中的ID名称,在我的类.java中可以使用任何方法给我这个示例ID吗?在这种情况下,我想要(text1和text2)。
谢谢,原谅英语。
解决方案
将ID设置为父母的布局,然后尝试这样的事情:
LinearLayout ll = findViewById(R.id.yourLayout);
for(int i=0; i<ll.getChildCount(); i++){
View v = ll.getChildAt(i);
int idView = v.getId();
//Do something with idView
}
其他提示
充气文件
linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View main = linflater.inflate(R.layout.main, null);
然后运行FindViewById将TextView转换为对象
TextView text1 = (TextView)main.findViewById(R.id.text1);
那么您可以稍后使用对象文本1
text1.setText("foo");
不隶属于 StackOverflow