简体   繁体   English

在 Android 中以编程方式在 Gridlayout 中创建 EditText

[英]Create EditText in Gridlayout programmatically in Android

I want to create editText everytime I clicked the button (create).每次单击按钮(创建)时,我都想创建 editText。 and Set the id, column and row.并设置id、列和行。

Gridlayout id: inputTasksLayout button id: addnewTask Gridlayout id:inputTasksLayout 按钮 id:addnewTask

When i clicked the button, it will create an editText in column 1, row 1 and assigned an id (task1).当我单击按钮时,它将在第 1 列第 1 行创建一个 editText 并分配一个 id (task1)。

When i clicked the button again, it will create another editText in column 1, row 2 and assigned an id (task2).当我再次单击该按钮时,它将在第 1 列第 2 行中创建另一个 editText 并分配一个 id (task2)。

When i clicked the button again, it will create another editText in column 1, row 2 and assigned an id (task3).当我再次单击该按钮时,它将在第 1 列第 2 行中创建另一个 editText 并分配一个 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.我尝试了其他来源,但它们都在 LinearLayout 中。 I want to put it in GridLayout.我想把它放在 GridLayout 中。

Yes of course it is possible.是的,当然有可能。 It is one of two main ways to create Views.它是创建视图的两种主要方式之一。 The good old code way...I prefer it over xml btw.好的旧代码方式......我更喜欢它而不是xml btw。 but thats just me.但这只是我。 I do nearly 90% of View creation and layouting in Code because I dont like the "dp" kind of way to say how wide or high a view ist for example....我在代码中完成了近 90% 的视图创建和布局,因为我不喜欢用“dp”这种方式来表达视图的宽度或高度,例如......

You just create a new EditText via您只需通过创建一个新的 EditText

new EditText();

Then you give it a height and a width.然后你给它一个高度和一个宽度。 Check for the methods you can use here http://developer.android.com/reference/android/widget/TextView.html (EditText inherits the TextView)检查您可以在此处使用的方法http://developer.android.com/reference/android/widget/TextView.html(EditText继承了 TextView)

And for your purpose check the Documentation of GridView out: http://developer.android.com/guide/topics/ui/layout/gridview.html http://developer.android.com/reference/android/widget/GridView.html为了您的目的,请查看 GridView 文档: http : //developer.android.com/guide/topics/ui/layout/gridview.html http://developer.android.com/reference/android/widget/GridView。 html

most importantly : http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews最重要的是: http : //developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews

the last link shows you how to insert an item into a layout like grid view for example.最后一个链接向您展示了如何将项目插入到布局中,例如网格视图。

It is as easy as XML but just needs a little bit more code.....它和 XML 一样简单,但只需要多一点代码.....

EDIT: As you keep on persist on a code solution keep in mind that stack overflow is not for demanding a code solution in your questions.编辑:当您继续坚持代码解决方案时,请记住堆栈溢出不是为了在您的问题中要求代码解决方案。 It is a plattform to get help not to receive specific solutions in code.它是一个获得帮助的平台,而不是在代码中接收特定的解决方案。

Check the opening text of my link:检查我的链接的开头文本:

"A layout defines the visual structure for a user interface, such as the UI for an activity or app widget. You can declare a layout in two ways: “布局定义了用户界面的视觉结构,例如活动或应用小部件的 UI。您可以通过两种方式声明布局:

Declare UI elements in XML.在 XML 中声明 UI 元素。 Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts. Android 提供了一个简单的 XML 词汇表,对应于 View 类和子类,例如那些用于小部件和布局的词汇。 Instantiate layout elements at runtime.在运行时实例化布局元素。 Your application can create View and ViewGroup objects (and manipulate their properties) programmatically.您的应用程序可以以编程方式创建 View 和 ViewGroup 对象(并操作它们的属性)。 The Android framework gives you the flexibility to use either or both of these methods for declaring and managing your application's UI. Android 框架使您可以灵活地使用这两种方法中的一种或两种来声明和管理应用程序的 UI。 For example, you could declare your application's default layouts in XML, including the screen elements that will appear in them and their properties.例如,您可以在 XML 中声明应用程序的默认布局,包括将出现在其中的屏幕元素及其属性。 You could then add code in your application that would modify the state of the screen objects, including those declared in XML, at run time."然后,您可以在应用程序中添加代码,以在运行时修改屏幕对象的状态,包括那些以 XML 声明的对象。”

So if you read the text you can adapt the things you will learn not only for edittext but for all views and its subclasses its not that hard....因此,如果您阅读文本,您可以调整您将学习的内容,不仅适用于编辑文本,而且适用于所有视图及其子类,这并不难......

Again SO is not for demanding explicit code solutions再次 SO 不是为了要求明确的代码解决方案

Yes can EditText View Dynamically on the click of Button Here below is the sample code.是的,单击按钮可以动态地 EditText 查看 下面是示例代码。

activity_main.xml活动_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主活动.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值文件夹中的 ids.xml

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM