Pro Android learning notes (15th): user interface and control (3): Button control

Source: Internet
Author: User
Basic Button

A Button is one of the most commonly used basic controls and is an inherited class of TextView in Android. ImageButton and ToggleButton are derived from above. We will introduce them one by one. Small example. We will mainly introduce how to trigger the basic Button. The Code is as follows:

Button bt = (Button) findViewById (R. id. ui_button1 );
Bt.SetOnClickListener(New onclicklistener (){
// This example is implemented through an internal Anonymous class. Of course, you can also reference the object of the view. onclicklistener interface for processing.

Public void onClick (View v ){
//... Specific trigger processing code
Log. d ("UiButtonTest", "Basic Button was clicked! ");
}
});

Android also provides another methodAndroid: onclickSpecify the trigger method name. The example is given in the inheritance class ImageButton of the Button.

ImageButton

The second row in the figure is ImageButton. The XML snippet is as follows:

<ImageButton android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"
Android: id = "@ + id/ui_imgbutton1"
Android: src = "@ drawable/ic_launcher"
<! -- Image source, under res/drawable -->
Android: onclick = "myclickfunc"
<! -- Specifies the trigger method after clicking the control -->

Android: Background = "@ null"/>
<! -- Set the background to null and remove the background of the button shape -->

<ImageButton android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"
Android: id = "@ + id/ui_imgbutton2"
Android: onclick = "myclickfunc"
<! -- In this example, the two buttons adopt the same trigger Method -->
Android: src = "@ drawable/ic_launcher"
/>

<ImageButton android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"
Android: Id = "@ + ID/ui_imgbutton3"
Android: contentdescription = "image button" <! -- Add the following descriptive language, but it is not displayed on the UI -->

Android: src = "@ drawable/ic_launcher"/>

The Code is as follows:

Public voidMyclickfunc(View v ){// Corresponds to Android: onclick in XML

Switch (V. GETID ()){
// Multiple controls agree to trigger. Code can be reused and the triggering of the control can be distinguished.
Case R. Id. ui_imgbutton1:
Case R. Id. ui_imgbutton2:
Log. D ("uibuttontest", "image button was clicked! Changed image ");

(Imagebutton) V ).Setimageresource(R. drawable. sample_image );
// Modify the imagebutton Image
Break;

Default:
Break;
}
}

ImageButton can also make some interesting changes and display different patterns in different situations. The third row in the small example is used to test this situation.

Set an xml file under res/drawable to describe the selection of slices in different situations. The example is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>
<SelectorXmlns: android = "http://schemas.android.com/apk/res/android">

<Item android: state_pressed = "true" android: drawable = "@ drawable/png03"/>

<Item android: state_focused = "true" android: drawable = "@ drawable/png02"/>
<Item android: drawable = "@ drawable/png01"/>

</Selector>

Note that the order of items in selector is exquisite. The system starts matching from the first and matches one by one until the result is successful. The png01 image is displayed by default. If this line is in the first position, it will be matched immediately, and other options will not work. The corresponding xml in res/layout is as follows:

<ImageButton android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/ui_button_selector "/>
<! -- Point to the xml file that is described in res/drawable -->

ToggleButton

ToggleButton is a switch button, which has two statuses: On and off. Xml is as follows:

<ToggleButtonAndroid: layout_width = "wrap_content"

Android: layout_height = "wrap_content"
Android: id = "@ + id/ui_toggle"
Android: textOn = "Runing" <! -- Display the on status -->
Android: textOff = "Stop"/> <! -- Display the off status -->

By default, the button is disabled at the beginning. Each time you press it, the button is switched on and off. You can also specify the switch status in the Code as follows:

ToggleButton tb = (ToggleButton) findViewById (R. id. ui_toggle );

Tb.SetChecked(True );

CheckBox

CheckBox and Button are very different from the user's perspective, but in Android, CheckBox is the inheritance class of android. widget. CompoundButton, and ComPoundButton is the inheritance class of android. widget. Button. XML is as follows:

<CheckBox android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: id = "@ + id/ui_cb_apple"
Android: text = "@ string/ui_cb_apple"
Android: checked = "true"/>
<CheckBox android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: id = "@ + id/ui_cb_banana"
Android: text = "@ string/ui_cb_banana"
Android: checked = "false"<! -- The default value is flase, which can be left unspecified -->

Android: onClick = "myClickFunc"/><! -CheckBox is a subclass of the Button, so you can use onClick to set the trigger Method -->

<CheckBox android: layout_width = "match_parent"

Android: layout_height = "wrap_content"
Android: id = "@ + id/ui_cb_melon"
Android: text = "@ string/ui_cb_melon"
Android: checked = "true"/>

CheckBox has two states: checked and unchecked. in code, you can set the status through setChecked (true | false), or you can modify the status through toggle. The application may be concerned with changing the checkbox status. The Code is as follows:

CheckBox cbApple = (CheckBox) findViewById (R. id. ui_cb_apple );

CbApple.SetOnCheckedChangeListener(New CompoundButton. OnCheckedChangeListener (){

Public voidOnCheckedChanged(CompoundButton buttonView, boolean isChecked ){

Log. d ("UiButtonTest", "CheckBox Apple state change to" + isChecked );

}
});

If (cbApple.IsChecked ()) // Check the checkbox status

CbApple.Toggle(); // Switch the checkbox status

CheckBox is a subclass of the Button. We can also use the setOnClickListener of the Button to implement it and define the click trigger method in xml. In this example, the public void myClickFunc (View v) is implemented.

RadioButton

RadioButton is one of the multiple containers and is encapsulated by the container RadioGroup. You can have only one option in a radiogroup. RadioButton is the CompoundButton inheritance class of the Button inheritance class, while RadioGroup is the LinearLayout inheritance class. RadioGroup has all the features of linear layout and can contain non-radioButton controls. In this example, a TextView is placed. The following is an xml snippet:

<RadioGroupAndroid: layout_width = "match_parent"

Android: layout_height = "wrap_content"
Android: id = "@ + id/ui_rbGroup"
Android: orientation = "vertical">
<TextViewAndroid: layout_width = "match_parent"

Android: layout_height = "wrap_content"
Android: text = "@ string/fruit"/>
<! -- Other controls can be placed in the RadioGroup container -->
<RadioButtonAndroid: layout_width = "match_parent"

Android: layout_height = "wrap_content"
Android: Id = "@ + ID/ui_rb_apple"
Android: text = "@ string/Apple"/>

<RadioButtonAndroid: layout_width = "match_parent"

Android: layout_height = "wrap_content"
Android: Id = "@ + ID/ui_rb_banana"
Android: text = "@ string/banana"
Android: checked = "true "/><! -- By default, RadioButton is unchecked. You can set checked for one of them. -->

<RadioButtonAndroid: layout_width = "match_parent"

Android: layout_height = "wrap_content"
Android: Id = "@ + ID/ui_rb_melon"
Android: text = "@ string/melon"/>"

</RadioGroup>

In the code, we can set the radiobutton status. The radio button that has been queried is checked, as shown below:

RadioGroup rbGroup = (RadioGroup) findViewById (R. id. ui_rbGroup );

RadioButton rbApple = (RadioButton) findViewById (R. id. ui_rb_apple );
RadioButton rbBanana = (RadioButton) findViewById (R. id. ui_rb_banana );
// Test: Set the RadioButton status
RbBanana.Toggle(); // Toggle () can change the state, but there are some differences in the radio button. You can change the State from unchecked to checked, but not vice versa (as a result, none of them are selected ), this statement does not work in this example, so toggle () must be used with caution.

RbBanana.SetChecked(False); // you can use setChecked () to set the status. This example runs to this sentence, all of which are in the unchecked status.

RbGroup.ClearCheck(); // You can also clear group. clearCheck (), all of which are unchecked.

RbApple. toggle (); // run this example to select Apple.

// Test: Add a new RadioButton in the Group using code.

RadioButton rbCherry = new RadioButton (this );
RbCherry. setText ("Cherry ");
RbCherry. setId (CHERRY_ID); // if not specified, the default value starts from 1. However, according to the programming principle, we should set it by ourselves.

RbGroup. addView (rbCherry );

// Test: Obtain the selected ID
Int checkId = rbGroup. getCheckedRadioButtonId (); // returns the R. id. ui_rb_apple

When the user changes the option, it can be triggered by registration, as shown below:

// [Note] It is recommended to specify a RadioGroup. For example, the control such as checkbox has been processed in the program, and the compilation will be considered a CompoundButton class. Therefore, if the program involves multiple controls, more accurate description should be provided

RbGroup.SetOnCheckedChangeListener(NewRadioGroup. OnCheckedChangeListener (){

@ Override
Public voidOnCheckedChanged(RadioGroup group, int checkedId ){

Log. d ("UiButtonTest", "Changed checked ID to" + checkedId );

Switch (checkedId ){
Case-1: // If-1 is all unchecked
Log. d ("UiButtonTest", "Choices cleared .");

Break;
Default:
RadioButton rb = (RadioButton) group. findViewById (checkedId );

Log. d ("UiButtonTest", "Chose" + rb. getText ());
Break;
}
}
});

Related links:
My Android development articles

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.