如何使用layout充气程序获取布局中对象的id或值



我试试Layout充气机,它很管用。然后,试图在布局视图中定义对象的id被夸大了。但是如何在这些布局中获取对象的值或id呢?此处为xml代码。。

XML隐藏.XML

<?xml version="1.0" encoding="utf-8"?>
android:id="@+id/hiddenLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Saya teks 1"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CheckBox1" />
<CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CheckBox2" />
<CheckBox
    android:id="@+id/checkBox3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CheckBox3" />
<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Satu" />
    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dua" />
    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tiga" />
</RadioGroup>

XML Main.XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <Button
        android:id="@+id/add"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:text="Add View" />
    <Button
        android:id="@+id/remove"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:text="Remove View" />
</LinearLayout>
<ScrollView
    android:id="@+id/ScrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <LinearLayout
        android:id="@+id/linearhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="TextView" />
</LinearLayout>
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="value" />

Java

private int jumlahteks = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    View buttonAdd = findViewById(R.id.add);
    buttonAdd.setOnClickListener(this);
    View buttonRemove = findViewById(R.id.remove);
    buttonRemove.setOnClickListener(this);
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId())
    {
    case R.id.add:
        jumlahteks = jumlahteks + 1;
    //Check if the Layout already exists
    //LinearLayout hiddenLayout = (LinearLayout)findViewById(R.id.hiddenLayout);
        //Inflate the Hidden Layout Information View
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout myLayout = (LinearLayout)findViewById(R.id.linearhost);
        View hiddenInfo = inflater.inflate(R.layout.hidden, myLayout, false);
        TextView myTextView = (TextView) hiddenInfo.findViewById(R.id.textView1);
        // Update the TextView Text
        myTextView.setId(jumlahteks);
        myTextView.setText(jumlahteks);
        myLayout.addView(hiddenInfo);
    //Get References to the TextView
    break;
    case R.id.remove:
        View myView = findViewById(R.id.hiddenLayout);
        ViewGroup parent = (ViewGroup) myView.getParent();
        parent.removeView(myView);
        break;
        // More buttons go here (if any) ...
    }

如何在hidden.xml中获取对象的id或值??并设置值,我使用这个布局充气器创建dinamycly视图,并获取视图中每个对象的值。谢谢你的帮助和建议。。

我不完全确定你想在这里做什么,但听起来你想在主布局中使用hiddenLayout,有时也希望它被隐藏。尝试在main.xml中的适当位置使用include标记包含hiddenLayout,使用android:id="为其设置名称,然后可以像在其他元素上一样使用findViewById。若要隐藏或显示它,请调用setVisibility(VISIBLE或INVISIBLE)。

编辑:您的hidden.xml没有根类型。看起来你也缺少了一个"<",所以也许你只是在复制/粘贴时没有包含它。我会创建一个Hidden.java类,它是一种布局类型的子类,比如LinearLayout,并具有您想要的字段的访问器。然后,您可以在xml中使用如下子类。然后你可以拨打

Hidden hiddenLayout = inflater.inflate(R.layout.hidden, null) 

获取隐藏的布局,然后使用hidden.getTextView1()之类的访问器来访问里面的东西。

您也可以将根类型设置为LinearLayout并使用

LinearLayout hiddenLayout = inflater.inflate(R.layout.hidden, null) 
TextView text = hiddenLayout.findViewById(R.id.TextView1) 

但是IMO子类化更好,因为它允许您为隐藏布局封装超出xml中可能的行为。

最新更新