Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

Create EditText in Gridlayout programmatically in Android

I want to create editText everytime I clicked the button (create). and Set the id, column and row.

Gridlayout id: inputTasksLayout button id: addnewTask

When i clicked the button, it will create an editText in column 1, row 1 and assigned an id (task1).

When i clicked the button again, it will create another editText in column 1, row 2 and assigned an id (task2).

When i clicked the button again, it will create another editText in column 1, row 2 and assigned an id (task3). And so on..

I want to know if its possible. If yes, can you give some sample codes? I tried the other sources but they are all in LinearLayout. I want to put it in GridLayout.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes can EditText View Dynamically on the click of Button Here below is the sample code.

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<GridLayout
    android:id="@+id/inputTasksLayout"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:columnCount="2"
    android:rowCount="10">


</GridLayout>

<Button
    android:id="@+id/addnewTask"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="addView"
    android:text="Add" />
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
int rowIndex = 1;
int colIndex = 0;
private GridLayout gridLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.inputTasksLayout);
    gridLayout = (GridLayout) findViewById(R.id.gridView);

}

public void addView(View view) {
    EditText editText = new EditText(this);
    GridLayout.LayoutParams param = new GridLayout.LayoutParams();
    param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    param.width = GridLayout.LayoutParams.MATCH_PARENT;
    param.rowSpec = GridLayout.spec(rowIndex);
    param.columnSpec = GridLayout.spec(colIndex);
    editText.setLayoutParams(param);
    if (rowIndex == 1) {
        editText.setId(R.id.task1);
    }
    if (rowIndex == 2) {
        editText.setId(R.id.task2);
    }

    gridLayout.addView(editText);
    rowIndex++;
 }
}

ids.xml in values folder

<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="task1" type="id" />
<item name="task2" type="id" />
<item name="task3" type="id" />
</resources>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...