我在我的主要布局的按钮的onClick我重复使用以下方法另一个XML布局:获取视图的ID,而resuing XML布局
View view = layoutInflater.inflate(R.layout.single_elemnt,
createLayout, false);
createLayout.addView(view, position);
position++;
其中single_elemnt
是将被添加到布局当前主布局每次点击add
按钮(出现在主布局中)。
single_elemnt
有2视图:a textview
和edittext
。我每次使用tv = (TextView) findViewById(R.id.tv);
时,点击了add
。
但问题是tv
总是指第一textview
即使我已经把findViewById
代码中的onClick。
main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- some other views -->
<Button
android:id="@+id/add"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:paddingTop="10dp"
android:text="Add"
android:textSize="20sp" />
</RelativeLayout>
在活动
View view = layoutInflater.inflate(R.layout.single_elemnt,
createLayout, false);
createLayout.addView(view, position);
position++;
tv = (TextView)profile.findViewById(R.id.tv);
tv.setText("some text");
的onClick方法难道我做错了什么吗?任何帮助赞赏。
请您发表您的代码'的onClick()'方法? – Vladimir 2013-04-30 08:04:42
请检查编辑的问题。 – GAMA 2013-04-30 08:15:03