因此,您有许多CardView,并且每个CardView都有一个包装的TextView,并且您想方便地从TextView访问所有这些名片视图。
我假设的TextView的id是textview1
和的CardView
是cardview1
然后,要CardView
使用其子级访问父级ID TextView
:
TextView textView1 = findViewById(R.id.textview1);
int id = ((CardView) textView1.getParent()).getId();
另请注意,您将永远无法获得cardview1
字符串,因为它以int值的形式存储在系统中。
为了确保您获得正确的ID,您需要进行一些检查,例如
if (((CardView) textView1.getParent()).getId() == R.id.cardview1)
Toast.makeText(this, "Cardview", Toast.LENGTH_SHORT).show();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句