在动态视图中添加动态视图

时间:2015-07-23 10:18:51

标签: android dynamic android-linearlayout

尝试使用动态功能创建一个表单来添加它。

下面是我的应用程序中创建表单的图像。

enter image description here

您可以在此页面中执行的操作。

  1. 绿色加号按钮 动态膨胀新的特定表单。为了清楚了解,将在静态表格下添加另一个酒店表格,绿色加号将在动态表格下方。我们可以添加多种形式

  2. 添加按钮对动态对进行充气,包括Cont。人和Cont.Number。请参阅下图以便清楚了解。我们可以添加多对。

  3. enter image description here

    当我通过按加号按钮获取成功的表单并且 ADD对按钮正在正常充气时,我发现我正面临的是当我添加第一个动态酒店表单时...当我通过按加号按钮获取成功的表格来添加第二个动态酒店表单时,当我按添加对按钮时,对在第一个动态表单中不是在第二。同样导致三种,四种动态形式......到n种形式。

    半贴pagetwo.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:weightSum="5" >
    
    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="2dp"
        android:layout_marginTop="10dp"
        android:layout_weight="5" >
    
        <LinearLayout
            android:id="@+id/main_trip_2_Layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <TextView
                android:id="@+id/hotel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="1.0dip"
                android:background="@drawable/button_back"
                android:gravity="left"
                android:padding="3.0dip"
                android:text=" * Hotel"
                android:textColor="@color/white" />
    
            <LinearLayout
                android:id="@+id/lin_lay_hotel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="17dp"
                android:orientation="vertical"
                android:visibility="visible" >
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:padding="2dp"
                    android:weightSum="3" >
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:text="Name"
                        android:textSize="12sp" />
    
                    <EditText
                        android:id="@+id/hotel_edit_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/edittextdesign"
                        android:imeOptions="actionNext|flagNoFullscreen"
                        android:inputType="textCapSentences" />
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:padding="2dp"
                    android:weightSum="3" >
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:text="Location"
                        android:textSize="12sp" />
    
                    <EditText
                        android:id="@+id/hotel_edit_location"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:autoLink="all"
                        android:background="@drawable/edittextdesign"
                        android:gravity="top|center_horizontal|left"
                        android:imeOptions="actionNext|flagNoFullscreen"
                        android:inputType="textCapSentences|textMultiLine"
                        android:lines="4"
                        android:scrollbars="vertical" />
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:padding="2dp"
                    android:weightSum="3" >
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:text="Room No / Name"
                        android:textSize="12sp" />
    
                    <EditText
                        android:id="@+id/hotel_edit_room_no_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/edittextdesign"
                        android:imeOptions="actionNext|flagNoFullscreen"
                        android:inputType="textCapSentences" />
                </LinearLayout>
    
                <LinearLayout
                    android:id="@+id/Layout_hotelContName"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:padding="2dp"
                    android:weightSum="3" >
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:text="Contact Person"
                        android:textSize="12sp" />
    
                    <EditText
                        android:id="@+id/hotel_edit_cont_person"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/edittextdesign"
                        android:imeOptions="actionNext|flagNoFullscreen"
                        android:inputType="textCapSentences" />
                </LinearLayout>
    
                <LinearLayout
                    android:id="@+id/Layout_hotelContNum"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:padding="2dp"
                    android:weightSum="3" >
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:text="Contact Number"
                        android:textSize="12sp" />
    
                    <EditText
                        android:id="@+id/hotel_edit_cont_number"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/edittextdesign"
                        android:imeOptions="actionNext|flagNoFullscreen"
                        android:inputType="phone" />
                </LinearLayout>
    
                <!--
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="right"
                    android:orientation="horizontal"
                    android:padding="2dp"
                    android:weightSum="3" >
    
                    <Button
                        android:id="@+id/hotel_cont_pair_delete"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/trip_add_delete_bg"
                        android:text="Delete"
                        android:textColor="#FFFFFF"
                        android:visibility="gone" />
                </LinearLayout>
                -->
    
                <!-- <ScrollView
                    android:id="@+id/hotel_scroll"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="0dp" > -->
    
                    <LinearLayout
                        android:id="@+id/hotel_cont_pair_linearLayoutFormtriptwo"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical" >
                    </LinearLayout>
                <!-- </ScrollView> -->
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="right"
                    android:orientation="horizontal"
                    android:padding="2dp"
                    android:weightSum="3" >
    
                    <Button
                        android:id="@+id/hotel_cont_pair_add"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/trip_add_delete_bg"
                        android:text="Add"
                        android:textColor="#FFFFFF" />
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:padding="2dp"
                    android:weightSum="3" >
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:text="Check In"
                        android:textSize="12sp" />
    
                    <EditText
                        android:id="@+id/hotel_edit_checkin"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/edittextdesign"
                        android:clickable="true"
                        android:focusable="false"
                        android:imeOptions="actionNext|flagNoFullscreen"
                        android:inputType="textCapSentences" />
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:padding="2dp"
                    android:weightSum="3" >
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:text="Check Out"
                        android:textSize="12sp" />
    
                    <EditText
                        android:id="@+id/hotel_edit_checkout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/edittextdesign"
                        android:clickable="true"
                        android:focusable="false"
                        android:imeOptions="actionNext|flagNoFullscreen"
                        android:inputType="textCapSentences" />
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:padding="2dp"
                    android:weightSum="3" >
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:text="Notes"
                        android:textSize="12sp" />
    
                    <EditText
                        android:id="@+id/hotel_edit_notes"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/edittextdesign"
                        android:imeOptions="actionNext|flagNoFullscreen"
                        android:inputType="textCapSentences" />
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:padding="2dp"
                    android:weightSum="10" >
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="3.33"
                        android:text="Cost"
                        android:textSize="12sp" />
    
                    <EditText
                        android:id="@+id/hotel_edit_cost"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="2.67"
                        android:background="@drawable/edittextdesign"
                        android:imeOptions="actionNext|flagNoFullscreen"
                        android:inputType="number" />
    
                    <EditText
                        android:id="@+id/hotel_edit_currency_type"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="2dp"
                        android:layout_weight="4"
                        android:background="@drawable/edittextdesign"
                        android:gravity="center"
                        android:imeOptions="actionNext|flagNoFullscreen"
                        android:inputType="textCapCharacters" />
                </LinearLayout>
    
                <!--
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="right"
                    android:orientation="horizontal"
                    android:padding="2dp" >
    
                    <ImageButton
                        android:id="@+id/h_addset"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:background="#ffffff"
                        android:src="@android:drawable/ic_delete"
                        android:visibility="visible" />
                </LinearLayout>
                -->
            </LinearLayout>
    
            <ScrollView
                android:id="@+id/scrollViewhotel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="17dp"
                android:layout_marginTop="10dp"
                android:visibility="gone" >
    
                <LinearLayout
                    android:id="@+id/hotellinearLayoutFormtriptwo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                </LinearLayout>
            </ScrollView>
    
            <ImageButton
                android:id="@+id/hotel_addset"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:background="#ffffff"
                android:src="@android:drawable/ic_input_add"
                android:visibility="visible" />
    

    必填代码SecondPageActivity.java

    绿色加按钮Onclick操作

    hotel_addset.setOnClickListener(new OnClickListener() {
            LinearLayout linearLayoutFormhotel = (LinearLayout) findViewById(R.id.hotellinearLayoutFormtriptwo);
    
            @Override
            public void onClick(View arg0) {
    
                AddhotelLayout(linearLayoutFormhotel);
    
            }
        });
    

    方法AddhotelLayout()

    @SuppressLint("InflateParams")
    protected void AddhotelLayout(final LinearLayout linearLayoutFormhotel) {
        // TODO Auto-generated method stub
    
        final LinearLayout hotelnewView = (LinearLayout)getLayoutInflater().inflate(R.layout.triptwo_hotelrowitem, null);
        hotelnewView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        ImageButton hotelbtnRemove = (ImageButton) hotelnewView.findViewById(R.id.hotelimagebuttonremove);
    
        Button hotel_cont_pair_add_dynamic = (Button) hotelnewView.findViewById(R.id.hotel_cont_pair_add_dynamic);
    
        hotel_cont_pair_add_dynamic.setOnClickListener(new OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
                final LinearLayout linearLayoutForm = (LinearLayout)findViewById(R.id.hotel_cont_pair_linearLayoutFormtriptwo_dynamic);
    
                // TODO Auto-generated method stub
                final LinearLayout newConatctPairView = (LinearLayout)getLayoutInflater().inflate(R.layout.trip_contactpair, null);
                newConatctPairView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
                Button delete_hotel_cont_pair = (Button) newConatctPairView.findViewById(R.id.commonPairRemove);
    
                delete_hotel_cont_pair.setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        linearLayoutForm.removeView(newConatctPairView);
                    }
                });
    
                linearLayoutForm.addView(newConatctPairView);
    
    
    
            }
    
    
        });
    
        //LinearLayout linearLayoutForm = (LinearLayout)activity.findViewById(R.id.hotel_cont_pair_linearLayoutFormtriptwo_dynamic);
    
        //hotelContPairAddDynamic(activity, hotel_cont_pair_add_dynamic);
    
        final EditText edithotelcheckin = (EditText) hotelnewView.findViewById(R.id.tripcreatetwoedit_checkin);
    
        edithotelcheckin.setTag("hotel_checkin");
    
        final EditText edithotelcheckout = (EditText) hotelnewView.findViewById(R.id.tripcreatetwoedit_checkout);
    
        edithotelcheckout.setTag("hotel_checkout");
    
        edithotelcheckin.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                updatecustom.showDialog(edithotelcheckin.getTag()
                        .toString());
    
            }
        });
    
        edithotelcheckout
        .setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                updatecustom.showDialog(edithotelcheckout
                        .getTag().toString());
            }
        });
    
        hotelbtnRemove.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                linearLayoutFormhotel.removeView(hotelnewView);
            }
        });
        linearLayoutFormhotel.addView(hotelnewView);
    
    }
    

    需要更多详细信息..?我会编辑我的Que。

1 个答案:

答案 0 :(得分:0)

例如,您正在动态插入视图,您可能拥有具有相同ID的不同视图。因此,当您搜索要添加final LinearLayout linearLayoutForm = (LinearLayout)findViewById(R.id.hotel_cont_pair_linearLayoutFormtriptwo_dynamic);的容器时,它会引用第一个视图而不是第二个视图。尽量不要在全局范围内使用findViewById,但要在动态添加的视图中使用

相关问题