問題描述
我正在開發一個應用程序,我在其中以編程方式創建一個 Edittext:
I am developing an application in which i am creating an Edittext programmatically as :
EditText edText = new EditText(this);
edText.setId(1);
edText .setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,0f));
edText .setInputType(InputType.TYPE_CLASS_NUMBER);
edText.setHint("dsgsdgsgs");
tableLayout.addView(edText);
在這里,我通過 edText.setId(1);
行將編輯文本的 id 設置為1",以整數表示.
Here I am setting the id of the Edit text as "1" by the line edText.setId(1);
in integer.
但我需要的是 - 我想以字符設置 ID,例如:
But what i need is - I want to set the ID in character as for example:
edText.setId("edittext_hello");
這樣我就可以通過該 ID 訪問它.我怎樣才能完成這項任務,請幫忙.
So that i can access it via that id. How can i achieve this task please help.
推薦答案
不能將 id 設置為 String.您只能將整數分配為 Id.但是,如果您想使用 String 作為 id 以便于使用,那么 -在 res/values/ids.xml 文件中
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
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="edit_text_hello" type="id"/>
</resources>
然后將其用作:
edText.setId(R.id.edit_text_hello);
所以你可以做你需要的.
So you can do what you need.
這篇關于Android:以編程方式設置編輯文本或文本視圖 ID的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!