How to Set Id in EditText programmatically in Android

。_饼干妹妹 提交于 2019-12-23 01:26:08

问题


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);

}

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++;
}

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

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

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

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

ANS SO ON.....


回答1:


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.

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. 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)

  2. 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);

You then get edittext's id by calling idsMap.get("task3")

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);



回答2:


You can keep references of these edit text in an array representing cells of your grid. declare arraylist like this:

ArrayList<EditText> etArray = new ArrayList<>();

and keep reference to your EditText in array list at the end of your addView method like this:

etArray.add(i,edittext);

now refer these view like this:

etArray.get(i);

this way you will be able to refer them for accessing text.

assigning ids dynamically can cause problems as id is an integer and your assigned ids may cause conflict with system assigned ids to other components.




回答3:


You can't set id as a String. You can only assign integer as Id. But if you want to use String as id for the ease of use then - in res/values/ids.xml file

<item name="edit_text_hello" type="id"/>

And then use it as:

edText.setId(R.id.edit_text_hello); So you can do what you need.



来源:https://stackoverflow.com/questions/32800418/how-to-set-id-in-edittext-programmatically-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!