在我的例子中,我正在添加新的LinearLayout,它通过按下按钮包含3个视图(TextView,EditText,Button)。
添加每个LinearLayout后我想循环遍历他的子视图,找到TextView和EditText并给它们一些愚蠢的文本。
但是,当我遍历LinearLayout子视图,并检查以查找哪个子视图是TextView以及哪个EditText时,看起来每个子视图都是TextView。
以下是LinearLayout外观的代码,xml文件名是:(field_konacno.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="A)"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:hint="Unesite pitanje"
android:textColorHint="@color/colorPrimaryDark"/>
<Button
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:background="@android:drawable/ic_delete"
android:onClick="deleteView" />
</LinearLayout>
以下是我的Main4Activity的外观:
public class Main4Activity extends AppCompatActivity {
private LinearLayout parentLinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
parentLinearLayout = (LinearLayout) findViewById(R.id.activity4_linearayout);
//creating first view
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field_konacno, null);
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount());
}
//adding new LinearLayout by pressing button
public void addNewLinearLayoutWithViews(View view) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field_konacno, null);
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount());
if(rowView instanceof LinearLayout){
LinearLayout linearLayout = (LinearLayout) rowView;
for (int i = 0; i<linearLayout.getChildCount(); i++){
if (linearLayout.getChildAt(i) instanceof TextView){
TextView someTetView = (TextView) linearLayout.getChildAt(i);
someTetView.setText("TV " + i);
}else if (linearLayout.getChildAt(i) instanceof EditText){
EditText editText = (EditText) linearLayout.getChildAt(i);
editText.setText("EditText " + i);
}else {
//do nothing
}
}
}
}
public void deleteView(View view) {
parentLinearLayout.removeView((View) view.getParent());
}
}
以下是我的activity_main4.xml的样子: 请注意,我已经通过xml添加onClick,而不是通过监听器。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.petar.addviewsdynamicly.Main4Activity">
<LinearLayout
android:id="@+id/activity4_linearayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dodaj polje"
android:onClick="addNewLinearLayoutWithViews"/>
</LinearLayout>
这是图片,这样你就可以看到,当我添加新的linearLayout时,他所有的子视图都是TextView。 在代码中我设置TextView来编写电视,在EditText中编写编辑文本
提前致谢!
答案 0 :(得分:2)
从EditText的条件开始。 EditText是TextView的子类,因此“instanceof TextView”为其实例返回true。
if (linearLayout.getChildAt(i) instanceof EditText){
EditText editText = (EditText) linearLayout.getChildAt(i);
editText.setText("EditText " + i);
} else if (linearLayout.getChildAt(i) instanceof TextView){
TextView someTetView = (TextView) linearLayout.getChildAt(i);
someTetView.setText("TV " + i);
}else {
//do nothing
}