Objective of this chapter
Master the usage of radio buttons Master the usage
of check boxes Master the usage
of switch buttons
Master the usage of image view.
Master the usage of auto-complete text box.
Radio Control-RadioButton An example of a common radio control
<RadioGroup android:layout_width=“wrap_content” android:layout_height=“wrap_content”><RadioButton android:id=“@+id/option1”android:layout_width=“wrap_content”android:layout_height=“wrap_content” android:text=“选项1” /><RadioButton android:id=“@+id/option2”android:layout_width=“wrap_content”android:layout_height=“wrap_content” android:text=“选项2” /></RadioGroup>
Example:
Select a favorite ball sport from a set of RadioButton lists, and display the result in the TextView after selection.
Summary: The relationship between RadioButton and RadioGroup:
1. RadioButton represents a single circular radio button, and RadioGroup is a container that can hold multiple RadioButtons
2. Only one RadioButton in each RadioGroup can be selected at the same time
3. The RadioButtons in different RadioGroups are not related to each other, that is, if one of group A is selected, one of group B can still be selected
4. In most cases, there are at least 2 RadioButtons in a RadioGroup
5. In most cases, one of the RadioButtons in a RadioGroup will be selected by default, and it is recommended that you place it at the beginning of the RadioGroup
Check control-CheckBox an example of a common check control
<CheckBox android:id=“@+id/checkbox1”android:layout_width=“wrap_content”android:layout_height=“wrap_content” android:text=“工作了吗?” />< CheckBox android:id=“@+id/checkbox2”android:layout_width=“wrap_content”android:layout_height=“wrap_content” android:text=“结婚了吗?” />
Example:
Add 3 check boxes and 1 button for hobbies on the screen; when a hobby is selected, the information will be output in the form of a log; when the submit button is clicked, all selected hobby items will be displayed.
ToggleButton is a button used to indicate the switch state. Use the ToggleButton label to declare in the layout file
<ToggleButton android:id="@+id/togglebtn"android:layout_width="wrap_content"android:layout_height=“wrap_content” />
Like buttons, use android.view.View.OnClickListener to listen to
common events and android.view.View. The
corresponding class of
OnCheckedChangeListener is android.widget.ToggleButton setTextOn() and setTextOff() methods can be used to set the button’s state text
setChecked () can be used to set the state of the button
getChecked() is used to extract the state of the button
ImageView is a view for displaying pictures
Can display pictures from other content providers.
Support various image formats
. The label in the XML layout file is ImageView. The commonly used attribute
android:src sets the image source to be displayed.
android:scaleType The filling method of the picture
android:adjustViewBounds Keep the aspect ratio
android:tint The
corresponding class for the coloring of the image is android.widget.ImageView
<ImageView android:id="@+id/imageview"android:layout_width="match_parent"android:layout_height="wrap_content"android:contentDescription="@string/hello_world"android:src="@drawable/dog"/>
ImageButton is a button that displays a picture.
You can specify the picture to be displayed by the button through android:src
<Button android:id="@+id/imagebutton"android:layout_width="match_parent"android:layout_height="wrap_content"android:src="@drawable/playbutton"/>The
corresponding class for listening to button events through android.view.View.OnClickListener is android.widget.ImageButton
ImageButton btn = (ImageButton)findViewById(R.id.ibtn);btn.setOnClickListener(new OnClickListener() { public void onClick(View v) { Log.d("imageview", "clicked"); }});ImageSwitcher is mainly used to display pictures and supports picture switching effects. It has similar functions to ImageView, but supports animation. The steps to build ImageSwitcher: Use ImageSwitcher for layout.
<ImageSwitcher android:id="@+id/imageswitcher"android:layout_width="match_parent"android:layout_height="400dip”></ImageSwitcher>
Steps to build ImageSwitcher:
1 Provide a view factory for ImageSwitcher in the code, which is used to display pictures 2
ImageSwitcher sets the animation of swapping in and swapping out imagesImageSwitcher is = (ImageSwitcher)findViewById(R.id.imageswitcher);is.setFactory(new ViewFactory() {public View makeView() {return new ImageView(TestImageActivity.this);}});is.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));is.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
Example: Complete a simple picture viewer
The auto-completion text box is an input component: when the user enters the initial content, it can automatically match the set subsequent content. It is a kind of auto-completion function similar to the AJAX technology in the Web. The component class: ndroid.widget.AutoCompleteTextView
When to use the autocomplete text box
There are many candidates and it is not suitable to use the drop-down box for selection.
Most of the time, users enter some fixed content to
help users enter quickly.
How to use it?
1. Provide a display layout for the automatically prompted drop-down selection item
2. Provide content data for the drop-down box
3. Use the auto-complete text box
.Auto-complete common properties of text boxes
android:completionHint
定义下拉菜单的提示信息
android:completionThreshold
定义在下拉显示提示前,用户输入的字符数量
android:dropdownHeight
指定显示提示的时候下拉框的高度作业:实现类似百度的搜索效果