读书笔记[Android Beginning 2] chapter 5 Employing Basic Widgets

来源:互联网 发布:大富豪5.3棋牌源码 编辑:程序博客网 时间:2024/04/29 21:44

Every GUI toolkit has some basic widgets: fields, labels, buttons, and so on.


Assigning Labels

The simplest widget is the label, referred to in Android as a TextView. As in most GUI toolkits, labels are bits of text that cannot be edited directly by users. Typically, they are used to identify adjacent widgets.

you can create a label by creating a TextView instance. More commonly, though, you will create labels in XML layout files by adding a TextView element to the layout, with an android:text property to set the value of the label itself.

TextView properties: TextView has many other attributes, you should refer to the Android SDK.

android:typeface: Sets the typeface to use for the label (e.g., monospace).
android:textStyle: Indicates that the typeface should be made bold (bold), italic (italic), or bold and italic (bold|italic).
android:textColor: Sets the color of the label’s text, in RGB hex format (e.g., #FF0000 for red).

Usage:

android:typeface="sans"

android:textStyle="bold|italic"

android:textColor="#ff0000"

Note: quotation marks are neccessory.


Button, Button, Who’s Got the Button?

Button is a subclass of TextView, so everything discussed in the preceding section also applies to formatting the face of the button.

However, Android 1.6 adds a new feature for the declaration of the “on-click” listener for a Button. In addition to the classic approach of defining some object (such as the activity) as implementing the View.OnClickListener interface, you can now take a somewhat simpler approach:

Define some method on your Activity that holds the button that takes a single View parameter, has a void return value, and is public.
In your layout XML, on the Button element, include the android:onClick attribute with the name of the method you defined in the previous step.

public void someMethod(View theButton) {
  // do something useful here
}
Then you could use this XML declaration for the Button itself, including
android:onClick:
<Button
 android:onClick="someMethod"
 ...
/>

注意:这种方法必须用在android 1.6以上

参考添加Listener的办法:http://hellsing42.iteye.com/blog/564772


Fleeting Images
Android has two widgets to help you embed images in your activities: ImageView and ImageButton. As the names suggest, they are image-based analogues to TextView and Button, respectively.

Each widget takes an android:src attribute (in an XML layout) to specify which picture to use. These usually reference a drawable resource
You can also set the image content based on a Uri from a content provider via setImageURI().
参考使用setImageURI的办法: http://stackoverflow.com/questions/3870638/how-to-use-setimageuri-android

Uri imgUri=Uri.parse("file:///data/data/MYFOLDER/myimage.png");


Fields of Green. Or Other Colors.
fields are the third anchor of most GUI toolkits. In Android, they are implemented via the EditText widget, which is a subclass of the TextView used for labels.

EditText has many other properties that will be useful to you in constructing fields, including the following:
android:autoText: Controls if the field should provide automatic spelling assistance.
android:capitalize: Controls if the field should automatically capitalize the first letter of entered text (useful for name or city fields, for example).
android:digits: Configures the field to accept only certain digits. android:singleLine: Controls if the field is for single-line input or multiple-line input (e.g., does pressing Enter move you to the next widget or add a newline?).


























原创粉丝点击