android:id 和 android:labelFor 之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24731137/
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
Difference between android:id and android:labelFor?
提问by CoDe
I wrote a simple layout which has an EditText
, but it's showing the following warning message:
我写了一个简单的布局,它有一个EditText
,但它显示了以下警告消息:
“No label views point to this text field”
“没有标签视图指向此文本字段”
While searching I found thisand it solved that warning message,
but did not get difference between both attributes android:id
and android:labelFor
. Any clarification?
在搜索时,我发现了这一点,它解决了该警告消息,但在属性android:id
和android:labelFor
. 任何澄清?
回答by My God
android:id
Supply an identifier name for this view, to later retrieve it with
View.findViewById()
orActivity.findViewById()
. This must be a resource reference; typically you set this using the @+ syntax to create a new ID resources. For example: android:id="@+id/my_id" which allows you to later retrieve the view withfindViewById(R.id.my_id)
.Must be a reference to another resource, in the form
"@[+][package:]type:name"
or to a theme attribute in the form "?[package:][type:]name".This corresponds to the global attribute resource symbol id.
安卓:id
提供此视图的标识符名称,以便稍后使用
View.findViewById()
或检索它Activity.findViewById()
。这必须是资源引用;通常,您使用 @+ 语法设置它以创建新的 ID 资源。例如: android:id="@+id/my_id" 允许您稍后使用findViewById(R.id.my_id)
.必须是对另一个资源的引用,
"@[+][package:]type:name"
以“?[package:][type:]name”的形式 或对主题属性的形式。这对应于全局属性资源符号 id。
android:labelFor
public static final int labelFor
Specifies the id of a view for which this view serves as a label for accessibility purposes. For example, a TextView before an EditText in the UI usually specifies what infomation is contained in the EditText. Hence, the TextView is a label for the EditText.
Must be an integer value, such as "100".
This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.
Constant Value: 16843718 (0x010103c6)
机器人:标签为
public static final int labelFor
指定视图的 ID,此视图用作可访问性目的的标签。例如,UI 中 EditText 之前的 TextView 通常指定 EditText 中包含哪些信息。因此,TextView 是 EditText 的标签。
必须是整数值,例如“100”。
这也可能是对包含此类型值的资源(以“@[package:]type:name”形式)或主题属性(以“?[package:][type:]name”形式)的引用.
常数值:16843718 (0x010103c6)
UPDATE:
更新:
For example -
例如 -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_width="match_parent">
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:labelFor="@+id/edit_item_name"
android:text="Item Name"/>
<EditText android:id="@+id/edit_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Item Name"/>
</LinearLayout>
</LinearLayout>
Reference: android:idand android:labelFor.
回答by Jitender Dev
The labelFor
is an attribute for accessibility options. You assign this to a label so that if, on a form , user clicks a EditText
field , android can know what to read (TalkBack for low vision users) to user.
该labelFor
是辅助功能选项的属性。您将其分配给一个标签,以便如果用户在表单上单击一个EditText
字段,android 就可以知道要向用户阅读什么(针对低视力用户的 TalkBack)。
The id
Supply an identifier name for this view, to later retrieve it with View.findViewById()
or Activity.findViewById()
.
该id
供应的标识符名称这一观点,到后来与检索它View.findViewById()
或Activity.findViewById()
。
回答by Henry
android:id
defines the ID of this view.
android:id
定义此视图的 ID。
android:labelFor
references the ID of another view.
android:labelFor
引用另一个视图的 ID。
回答by Benny
If an app's user interface already provides a text label for the editable item, define
android:labelFor
on the labelingView
to indicate which item the label describes.
如果应用程序的用户界面已经为可编辑项目提供了文本标签,请
android:labelFor
在标签View
上定义以指示标签描述的项目。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/email_subject_label"
android:labelFor="@id/email_subject" />
<EditText
android:id="@+id/email_subject"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
For more details: Android Accessibility - How to provide content labels
有关更多详细信息:Android 辅助功能 - 如何提供内容标签
回答by ceph3us
in addition to all answers if u don't use xml files for apps this is a brief explanation what for serves VIEW ID :
除了所有的答案,如果你不为应用程序使用 xml 文件,这是一个简短的解释,用于服务 VIEW ID:
(btw in my opinion using xml sucks - my only xml file is manifest :D generated by gradle)
(顺便说一句,我认为使用 xml 很糟糕——我唯一的 xml 文件是清单 :D 由 gradle 生成)
@IdRes - annotation for resource id
@IdRes - 资源 id 的注释
/** define resource id for view */
@IdRes
int TEXT_VIEW_ID = "111111";
/** create edit tex in code */
EditText myTextView = new EditText(Context);
/** set view id */
myTextView.setID(TEXT_VIEW_ID);
/** set layout params etc then attach or inflate as u wish to view hierarchy */
/** use view id to find view */
EditText etFound = (EditText) View.findViewById(TEXT_VIEW_ID);
ps. ID is mandatory to preserve state of hierarchy view when Activity.onSaveInstanceState(Bundle) is used - so if u create in code (VIEW / WIDGET / LAYOUT etc.) don't forget to set it.
附:当使用 Activity.onSaveInstanceState(Bundle) 时,ID 是保留层级视图状态所必需的 - 所以如果你在代码中创建(VIEW / WIDGET / LAYOUT 等)不要忘记设置它。