Android:以编程方式设置编辑文本或文本视图 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22527883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Set Edit text or text view id Programmatically
提问by Optim India
I am developing an application in which i am creating an Edittext programmatically as :
我正在开发一个应用程序,我在其中以编程方式创建 Edittext 为:
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);
Here I am setting the id of the Edit text as "1" by the line edText.setId(1);
in integer.
在这里,我通过edText.setId(1);
整数行将编辑文本的 id 设置为“1” 。
But what i need is - I want to set the ID in character as for example:
但我需要的是 - 我想将 ID 设置为字符,例如:
edText.setId("edittext_hello");
So that i can access it via that id. How can i achieve this task please help.
这样我就可以通过该 ID 访问它。我怎样才能完成这项任务,请帮忙。
回答by Dori
As the others have said you can't do this. Why do you want to / what's your requirement?
正如其他人所说,你不能这样做。你为什么要/你的要求是什么?
You could create an idin an xml file and use that - if you want it to be descriptive. This is also a better approach than using literal int
s as you may get an idclash with other views in the layout hierarchy (unlikely but possible). This to me seems like the best / cleanest solution to your problem.
您可以在 xml 文件中创建一个id并使用它 - 如果您希望它具有描述性。这也是比使用文字int
s更好的方法,因为您可能会与布局层次结构中的其他视图发生id冲突(不太可能但可能)。在我看来,这似乎是您问题的最佳/最干净的解决方案。
See http://developer.android.com/guide/topics/resources/more-resources.html#Id
请参阅http://developer.android.com/guide/topics/resources/more-resources.html#Id
e.g. in res/values/id.xml
例如在 res/values/id.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item
type="id"
name="edittext_hello" />
</resources>
and then set with
然后设置
edText.setId(R.id.edittext_hello);
回答by Manoj Fegde
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.xmlfile
您不能将 id 设置为字符串。您只能将整数指定为 Id。但是如果你想使用 String 作为 id 以方便使用,那么 - 在res/values/ids.xml文件中
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="edit_text_hello" type="id"/>
</resources>
And then use it as:
然后将其用作:
edText.setId(R.id.edit_text_hello);
So you can do what you need.
所以你可以做你需要的。
回答by user370305
No, you can't set id
as a String. You can only assign integer
as Id. You can use setTag()
of View
with String. But for id it will only Integer. As android resources are maintained in R.java file for integer type.
不,您不能设置id
为字符串。您只能分配integer
为 Id。您可以使用setTag()
的View
带有字符串。但是对于 id,它只会是整数。由于android资源在整数类型的R.java文件中维护。
Update:
更新:
Why do you not set String or other data types (without integer) id to any android resource?
为什么不将 String 或其他数据类型(无整数)id 设置为任何 android 资源?
Because:
因为:
An Android Resource id is a 32-bit integer. It comprises
Android 资源 ID 是一个 32 位整数。它包括
an 8-bit Package id [bits 24-31]
an 8-bit Type id [bits 16-23]
a 16-bit Entry index [bits 0-15]
The Package ididentifies the Package chunk which contains the Resource.
该包ID标识的包装块包含的资源。
The Type ididentifies the type of the Resource and hence the corresponding Typespec chunk and Type chunk or chunks which contain its value or value(s)
的类型ID标识资源的类型并且因此相应的类型指定块和类型块或包含它的值或值(多个)块
The Entry indexidentifies the individual Resource within the Typespec chunk and Type chunk(s).
的条目索引标识的类型指定块和块类型(一个或多个)内的各个资源。
回答by Hamid Shatu
You can't set id
with char
, String
or anything else except int
...because, id
is maintained by R.java
file which contains only int
.
你可以不设置id
与char
,String
或任何其他除外int
......因为,id
是维护R.java
只包含文件int
。
You can use setTag()
instead of setId()
.
您可以使用setTag()
代替setId()
.
Use setTag()
as below...
使用setTag()
如下...
edText.setTag("edittext_hello");
回答by FLBKernel
You can also achieve your problem without creating any extra resource like ids.xml and without having any id set to any of the EditText created dynamically.
您还可以解决您的问题,而无需创建任何额外的资源,如 ids.xml,也无需将任何 id 设置为动态创建的任何 EditText。
Just need to have the parent reference of the element you have set a tag, and then, you can find it with the tag.
只需要有你设置标签的元素的父引用,然后,你就可以通过标签找到它。
Parent
家长
LinearLayout linearLayout= (LinearLayout) findViewById(R.id.layout_id);
LinearLayout linearLayout= (LinearLayout) findViewById(R.id.layout_id);
and then
进而
Childs
柴尔兹
EditText editText = (EditText) linearLayout.findViewWithTag("yourStringTag");
EditText editText = (EditText) linearLayout.findViewWithTag("yourStringTag");
for example..
例如..
Note: Obviously, if you have created EditTexts dynamically, you would do something like this:
注意:显然,如果您动态创建了 EditTexts,您将执行以下操作:
for (Map.Entry<String, String> entry : yourHashMap.entrySet()) {
LinearLayout linearLayout= (LinearLayout) findViewById(R.id.parent_layout_id);
EditText editText = (EditText) linearLayout.findViewWithTag(entry.getKey());
// Then do whatever you want with your editText referenced.
// ...
}
回答by Lakshay Sharma
You can use setText() instead of setId().
您可以使用 setText() 而不是 setId()。
edText_view.setText("Your Text");
edText_view.setText("你的文字");