Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

What is "android.R.id.text1"?

I am new to Android development. In the Notepad sample, I saw the following code snippet:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.noteslist_item, cursor,
        new String[] { Notes.TITLE }, new int[] { android.R.id.text1 });

and in the notelist_item.xml file:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"   <-----------HERE
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="5dip"
    android:singleLine="true"
/>

So, I am wondering, what is this android.R.id.text1?

Also, I found android.R.id.button1, button2, button3 in the android.jar file.

Are they some kind of well known IDs for some 3rd party controls?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

android.R.id.text1 is just an identifier defined in the Android framework.

In the framework, android.R.id.text1 is an used for TextView views. You can find it in many layouts from the framework (select_dialog_item, select_dialog_singlechoice, simple_dropdown_item_1line, etc.). In Android framework xml, it is represented by @+id/text1.

Hence, if you use one of these layouts and want to change the text, you will need to use this id.

// probably in a custom ListAdapter that uses 
View view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
TextView textView = (textView) view.findViewById(android.R.id.text1);
textView.setText("Oh no! not hello world again");

Also, you can use this same identifier to identify a TextView (or anything) in your custom layouts. See in the sample "Notepad", the layout file noteslist_item.xml.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:singleLine="true"  
/>

And actually, you could use R.id.text1 as an identifier of anything else, but that would be confusing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...