簡體   English   中英

無法將EditText動態添加到LinearLayout

[英]Unable to dynamically add EditText to LinearLayout

我讀過很多類似的帖子,但是沒有一個解決我的問題。 另一個有趣的事情是,在模擬器上保留了edittext的位置(這意味着將其他小部件向下移動),但未顯示,另一方面,在真實電話上的相同實現甚至沒有顯示該小部件的位置。 。 我具有以下LinearLayout ,當單擊TextView 添加團隊成員時,我需要在其中動態添加EditText小部件。

<LinearLayout
            android:id="@+id/layout_add_task"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/separator"
            android:layout_margin="5dp"
            android:background="@color/yellow_background"
            android:orientation="vertical"
            android:padding="5dp">

            <LinearLayout
                android:id="@+id/layout_task_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageView
                    android:id="@+id/iv_check_research_tasks"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/icon_check_empty" />

                <EditText
                    android:id="@+id/et_task_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:selectAllOnFocus="true"
                    android:text="Task Name"
                    android:textSize="20dp"
                    android:textStyle="bold" />
            </LinearLayout>

            <TextView
                android:id="@+id/tv_add_team_member"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_marginTop="5dp"
                android:text="Add a team member"
                android:textColor="@color/text_green"
                android:textSize="15dp"
                android:textStyle="bold" />

            <LinearLayout
                android:id="@+id/ll_add_task"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/tv_add_team_member"
                android:orientation="horizontal"
                android:weightSum="100" >

                <ImageView
                    android:id="@+id/iv_create_assignment_attach"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="10"
                    android:src="@drawable/icon_flag_alt" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="5dp"
                    android:layout_weight="15"
                    android:text="Due"
                    android:textSize="15dp" />

                <EditText
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="65"
                    android:hint="None"
                    android:selectAllOnFocus="true" />

                <ImageView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="10"
                    android:src="@drawable/icon_calendar_empty" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_horizontal"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/btn_create_assignment_trash"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="30dp"
                    android:layout_marginTop="5dp"
                    android:text="Trash" />

                <Button
                    android:id="@+id/btn_create_assignment_done"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp"
                    android:layout_marginTop="5dp"
                    android:text="Done" />
            </LinearLayout>
        </LinearLayout>

這是Java代碼,即TextView的onClickListener實現。

tvAddTeamMember.setOnClickListener(new OnClickListener() {

        @Override

        public void onClick(View v) {
            LinearLayout ll = (LinearLayout) getActivity().findViewById(R.id.layout_task_name);
            EditText et = new EditText(getActivity());
            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            et.setLayoutParams(p);
            et.setText("Text");
            ll.addView(et);

        }

您可以這樣進行! 這不是您要找的東西。 但是您的問題將得到解決。

1)默認情況下,在XML中添加EditText視圖,使其不可見。

2)在單擊TextView時,使其可見。

請享用 !

嗨@Anas Azeem:您的代碼是正確的。通過您的代碼,我們可以將editetxt動態添加到線性布局中。 問題僅在布局文件中。在布局中,將添加布局的方向稱為“水平”,而不是如下所示使用“垂直”

<LinearLayout
            android:id="@+id/layout_task_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/iv_check_research_tasks"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />

            <EditText
                android:id="@+id/et_task_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:selectAllOnFocus="true"
                android:text="Task Name"
                android:textSize="20dp"
                android:textStyle="bold" />
        </LinearLayout>

嘗試更改該行:

 LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

有了這個:

  android.widget.LinearLayout.LayoutParams p= new android.widget.LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
 //Then do your work

如果知道您可能要添加布局,則可以始終聲明ViewStub。 之后,您只需找到viewStub並對其調用inflate()即可:

ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();

這是在Android SDK中執行此操作的升級方法。 因此,您可以在xml文件中定義基本布局。

嘗試使用此代碼。 替換您的行ll.addView(et);

創建人:ll.addView(et,p);

並刪除您的行et.setLayoutParams(p);

一種簡單的方法。

        Button add = (Button) findViewById(R.id.add_checkpoint);
        final LinearLayout layout = (LinearLayout) findViewById(R.id.addLayout);        
        // layout has assigned weight = "1.0" in xml & it's a child of ScrollView
        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater oLayoutInflater = getLayoutInflater();
                // oView is declared globally & Layout "row_checkpoint" contains a EditText
                oView = oLayoutInflater.inflate(R.layout.row_checkpoint, null);

                final EditText speciesEditText = (EditText) oView
                        .findViewById(R.id.checkpoint); 
                speciesEditText.setId(layout.getChildCount());
                // Perform whatever required over EditText, same way you can add more elements.
                layout.addView(oView);
            }
        });

希望它會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM