Академический Документы
Профессиональный Документы
Культура Документы
Chapter
23
24
The class name of the widget, Button, forms the name of the XML element. Since Button is an Android-supplied widget, we can just use the bare class name. If you create your own widgets as subclasses of android.view.View, you will need to provide a full package declaration as well (e.g., com.commonsware.android.MyWidget). The root element needs to declare the Android XML namespace:
xmlns:android="http://schemas.android.com/apk/res/android"
All other elements will be children of the root and will inherit that namespace declaration.
25
Because we want to reference this button from our Java code, we need to give it an identifier via the android:id attribute. We will cover this concept in greater detail in the next section. The remaining attributes are properties of this Button instance: android:text: Indicates the initial text to be displayed on the button face (in this case, an empty string). android:layout_width and android:layout_height: Tell Android to have the button's width and height fill the parentin this case, the entire screen. These attributes will be covered in greater detail in Chapter 6. Since this single widget is the only content in our activity, we need just this single element. Complex UIs will require a whole tree of elements, representing the widgets and containers that control their positioning. All the remaining chapters of this book will use the XML layout form whenever practical, so there are dozens of other examples of more complex layouts for you to peruse.
This is the same setContentView() we used earlier, passing it an instance of a View subclass (in that case, a Button). The Android-built View, constructed from our layout, is accessed from that code-generated R class. All of the layouts are accessible under
26
R.layout, keyed by the base name of the layout file; for example, res/layout/main.xml results in R.layout.main. To access your identified widgets, use findViewById(), passing in the numeric identifier of the widget in question. That numeric identifier was generated by Android in the R class as R.id.something (where something is the specific widget you are seeking). Those widgets are simply subclasses of View, just like the Button instance we created in the previous chapter.
public class NowRedux extends Activity implements View.OnClickListener { Button btn; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); btn=(Button)findViewById(R.id.button); btn.setOnClickListener(this); updateTime(); } public void onClick(View view) { updateTime(); } private void updateTime() { btn.setText(new Date().toString()); } }
The first difference is that, rather than setting the content view to be a view we created in Java code, we set it to reference the XML layout (setContentView(R.layout.main)).
27
The R.java source file will be updated when we rebuild this project to include a reference to our layout file (stored as main.xml in our projects res/layout directory). The other difference is that we need to get our hands on our Button instance, for which we use the findViewById() call. Since we identified our button as @+id/button, we can reference the buttons identifier as R.id.button. Now, with the Button instance in hand, we can set the callback and set the label as needed. The results look the same as with the original Now demo, as shown in Figure 41.