如何在Android中以编程方式在EditText中设置ID [英] How to Set Id in EditText programmatically in Android

查看:418
本文介绍了如何在Android中以编程方式在EditText中设置ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GridLayout上有一个名为addnewTask的按钮.创建此按钮时,它将创建一个EditText.

I have this button on GridLayout called addnewTask. When you create this button, it will create an EditText.

private GridLayout gridLayout;

int rowIndex = 3;
int colIndex = 1;

int i=0;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_set_task);

    gridLayout = (GridLayout) findViewById(R.id.taskLayout);

}

此功能可在单击按钮时创建EditText->

This function to create EditText when the button is clicked -->

public void addView(View view) {
    i++;
    String tname = "task" + Integer.toString(i);
    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 > 3) {
        editText.setTag(tname);
    }

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

我的问题是我想设置我创建的EditText的android:id.

My problem is that i want to set the android:id of EditText i created.

像这样:单击按钮时,将在第3行第1列和ID名称task1中创建EditText.

like this: When the button is clicked, EditText is created, in row 3, column 1 and id name task1.

再次单击该按钮时,将在第4行第1列和ID名称task2中创建另一个EditText.

When the button is clicked again, another EditText is created, in row 4, column 1 and id name task2.

再次单击该按钮时,将在第5行第1列和ID名称task3中创建另一个EditText.

When the button is clicked again, another EditText is created, in row 5, column 1 and id name task3.

ANS SO ON .....

ANS SO ON.....

推荐答案

android中的ID不是字符串-它们始终是数字.即使您用xml @+id/textId编写,也会为该文本生成一个数字.您可以在R文件中看到它.

Ids in android aren't strings - they are always numbers. Even if you write in xml @+id/textId, a number is generated for this text. You can see that in your R file.

您可以做的是使用editText.setId(int)方法将ID分配给您的编辑文本.如果您希望能够轻松参考编辑文本,则可以:

What you can do is assign id to your edit texts by using editText.setId(int) method. If you want to be able to easily refer to the edit texts, you can either:

  1. 依次分配ID:1、2、3等.然后该项目的ID为(row-1) * <columnsCount> + column)(因此,如果您有3列,则第五行的第二个项目的ID为4 * 3 + 2)

  1. assign the ids sequentially: 1, then 2, 3 etc. Then id of the item would be (row-1) * <columnsCount> + column) (so if you have 3 columns, then second item in fifth row would have id 4 * 3 + 2)

创建类型为Map<String, Integer>的地图字段,然后再次顺序分配ID,并将其保存在其中.

create a map field of type Map<String, Integer>, and again assigns ids sequentially, and save them in.

String tname = "task" + Integer.toString(i);   
EditText editText = new EditText(this);
editText.setId(i);
idsMap.put(tname, i);

然后您可以通过调用idsMap.get("task3")

第三种选择是在地图中仅引用您的EditText:您将拥有一个Map<String, EditText>地图,然后调用

Third option is to just keep reference to your EditText in a map: you'd then have a Map<String, EditText> map, and then call

String tname = "task" + Integer.toString(i);   
EditText editText = new EditText(this);
editTextsMap.put(tname, editText);

这篇关于如何在Android中以编程方式在EditText中设置ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆