
我有一个Android活动,其中有八个TextVIEw,分别名为tvstat1,tvstat2,…,tvstat8.我有一个函数,需要一个整数作为参数.我想做的是这样的:
public voID incrementscore(int Statisticcategory){ String s "R.ID.tvstat" + String.ValueOf(Statisticcategory); TextVIEw tvGeneric = (TextVIEw)findVIEwByID(s); // ... do something with the text in the generic TextVIEw...}但是当然,这是行不通的,因为findVIEwByID方法仅采用整数作为参数,因此不喜欢我基于传入参数识别通用TextVIEw的方式.由于我只有八个TextVIEw,因此无需花费太多精力编写switch语句……但是我认为必须有一种更好的方法.有任何想法吗?
解决方法:
您可以使用ViewGroup.getChildCount()和ViewGroup.getChildAt().这是一个示例.
假设您有布局:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" > <linearLayout androID:ID="@+ID/text_group" androID:orIEntation="vertical" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <TextVIEw androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" /> <TextVIEw androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" /> <TextVIEw androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" /> <TextVIEw androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" /> </linearLayout></linearLayout>您可以使用下一个代码将文本分配给TextVIEws:
@OverrIDepublic voID onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); linearLayout textGroup = (linearLayout)findVIEwByID(R.ID.text_group); for(int i = 0; i < textGroup.getChildCount(); i++) { TextVIEw text = (TextVIEw)textGroup.getChildAt(i); text.setText("This is child #"+i); }}
总结以上是内存溢出为你收集整理的android-一般访问TextView全部内容,希望文章能够帮你解决android-一般访问TextView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)