Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:以编程方式设置编辑文本或文本视图id_Android_Android Layout_Android Edittext - Fatal编程技术网

Android:以编程方式设置编辑文本或文本视图id

Android:以编程方式设置编辑文本或文本视图id,android,android-layout,android-edittext,Android,Android Layout,Android Edittext,我正在开发一个应用程序,其中我以编程方式创建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_NUMB

我正在开发一个应用程序,其中我以编程方式创建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);
在这里,我通过
edText.setId(1)行将编辑文本的id设置为“1”以整数表示

但我需要的是-我想在字符中设置ID,例如:

edText.setId("edittext_hello");

因此,我可以通过该id访问它。我如何才能完成此任务,请帮助。

您不能使用
char
String
或除
int
以外的任何内容设置
id
。因为,
id
R.java
文件维护,该文件只包含
int

您可以使用
setTag()
而不是
setId()

使用
setTag()
如下所示

edText.setTag("edittext_hello");

不,不能将
id
设置为字符串。只能将
integer
指定为Id。可以将
View
setTag()
与字符串一起使用。但对于id,它将仅为整数。由于android资源在R.java文件中维护为整数类型

更新:

为什么不将字符串或其他数据类型(不带整数)id设置为任何android资源?

因为:

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]
包id标识包含资源的包块

类型id标识资源的类型,从而标识相应的Typespec块和 键入包含其值的chunk或chunk


条目索引标识Typespec块和类型块中的单个资源

正如其他人所说,你不能这样做。你为什么想/你的要求是什么

您可以在xml文件中创建一个id并使用它——如果您希望它是描述性的话。这也是一种比使用literal
int
s更好的方法,因为您可能会与布局层次结构中的其他视图发生id冲突(不太可能,但可能)。在我看来,这似乎是解决你问题的最好/最干净的办法

e、 g.在
res/values/id.xml中

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

不能将id设置为字符串。您只能将整数指定为Id。但是,如果为了便于使用,您想使用字符串作为Id,那么- 在res/values/ids.xml文件中

<?xml version="1.0" encoding="utf-8"?>
<resources>

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

</resources>
因此,您可以执行所需操作。

您可以使用setText()而不是setId()


edText_view.setText(“您的文本”)

您也可以在不创建任何额外资源(如ids.xml)和不将任何id设置为任何动态创建的EditText的情况下解决问题

只需要将元素的父引用设置为标记,然后就可以使用标记找到它

家长

LinearLayout LinearLayout=(LinearLayout)findViewById(R.id.layout\u id)

然后

儿童

EditText EditText=(EditText)linearLayout.findViewWithTag(“yourStringTag”)

例如

注意:显然,如果您已经动态创建了EditText,您将执行以下操作:

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

}
for(Map.Entry:yourHashMap.entrySet()){
LinearLayout LinearLayout=(LinearLayout)findViewById(R.id.parent\u layout\u id);
EditText EditText=(EditText)linearLayout.findViewWithTag(entry.getKey());
//然后对引用的editText执行任何操作。
// ...
}

看这是上面那个多莉的答案的副本!真的不值得添加吗?我同意:(它被标记为答案-有点烦人!如果您根据属性文件动态创建了元素怎么办?如何将属性文件中的ID动态设置为创建的每个元素?好的,我已经找到了方法。只需将元素的父引用设置为标记,然后你可以通过标签找到它。
LinearLayout LinearLayout=(LinearLayout)findViewById(R.id.sale\u aditionals\u layout);
然后
MaterialEditText MaterialEditText=(MaterialEditText)LinearLayout.findViewWithTag(entry.getKey())
例如..如果您根据属性文件动态创建了元素怎么办?如何将属性文件中的ID动态设置为创建的每个元素?好的,我已经找到了方法。只需将元素的父引用设置为标记,然后就可以使用标记找到它。
LinearLayout LinearLayout=(LinearLayout)findViewById(R.id.sale\u aditionals\u layout);
然后
MaterialEditText MaterialEditText=(MaterialEditText)LinearLayout.findViewWithTag(entry.getKey());
例如,问题是关于如何添加id而不是文本。
edText.setId(R.id.edit_text_hello);
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.
// ...

}