Android Study Notes (4) CheckBox, RadioGroup, and ProgressBar

Source: Internet
Author: User

1. Select multiple buttons-CheckBox
Usage: first, the Control ID is used to obtain the object representing the control, and then the listener is added to it.
Set the listener code:
Product_box. setOnCheckedChangeListener (new CompoundButton. onCheckedChangeListener () {@ Override public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stub if (isChecked) {System. out. println ("swim is checked");} else {System. out. println ("swim is unchecked ");}}}); <divre mce_tmp = "1"> <pre> </pre> <p> <font class = "" style = "FONT-FAMILY:" color = "# ff7e0 0 "> <p> the consumer box is a subclass of CompoundButton, so it can be converted to the CompoundButton type and the parameter is passed in. </P> </font> </p> </divre>
 
 
 
 
Ii. Single-choice button-RadioGroup
Usage: Same as CheckBox
Set the listener code:
GenderGroup. setOnCheckedChangeListener (new RadioGroup. onCheckedChangeListener () {@ Override public void onCheckedChanged (RadioGroup group, int checkedId) {// TODO Auto-generated method stub if (femaleButton. getId () = checkedId) {System. out. println ("famale"); Toast. makeText (RadioTest. this, "famle", Toast. LENGTH_SHORT ). show ();} else if (maleButton. getId () = checkedId) {System. out. println ("male") ;}}); <divre mce_tmp = "1"> <pre> </divre>
 
 
 
 
 
 
 
3. Progress bar-ProgressBar
The. xml file is defined as follows: horizontal progress bar
<ProgressBar android: id = "@ + id/firstBar" style = "? Android: attr/progressBarStyleHorizontal "android: layout_width =" 200dp "android: layout_height =" wrap_content "android: visibility =" gone "// whether the status is visible, gone is not visible/> <divre mce_tmp = "1"> <pre> </pre> <p> <font class = "" style = "FONT-FAMILY: "color =" #464646 "> <p> the maximum progress bar value is 100 by default. In the preceding example, to customize the progress bar, use android: max =" xxx, you can also set it in the code. Example! </P> </font> </p> </divre>
 
 
 
 
Let's look at another widget.
<ProgressBar android: id = "@ + id/secondBar" style = "? Android: attr/progressBarStyle "// here, progressBarStyle is the default style, that is, the circle droid: layout_width =" wrap_content "android: layout_height =" wrap_content "android: visibility = "gone"/> <divre mce_tmp = "1"> <pre> </pre> <p> </divre>
 
Progress bar in Activity:

Class ButtonListener implements OnClickListener {@ Override public void onClick (View v) {if (I = 0) {// set the progress bar to firstBar in the visible state. setVisibility (View. VISIBLE); firstBar. setMax (150); // set the maximum value of the progress bar in the Code secondBar. setVisibility (View. VISIBLE);} else if (I <firstBar. getMax () {// set the current value of the main progress bar firstBar. setProgress (I); // set the current value of the Second progress bar firstBar. setSecondaryProgress (I + 10); // The progress bar cannot be displayed due to the default progress bar. // secondBar. setProgress (I);} else {// set the progress bar to the invisible firstBar. setVisibility (View. GONE); secondBar. setVisibility (View. GONE) ;} I = I + 10 ;}}
<Divre mce_tmp = "1"> </divre>
 
 
Iv. List-ListView
 
The List implements ListActivity instead of Activity.
Activity Code:
 
Java code
Public class Activity01 extends ListActivity {
/** Inherit from the ListActivity class */
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
ArrayList <HashMap <String, String> list = new ArrayList <HashMap <String, String> ();
HashMap <String, String> map1 = new HashMap <String, String> ();
HashMap <String, String> map2 = new HashMap <String, String> ();
HashMap <String, String> map3 = new HashMap <String, String> ();
Map1.put ("user_name", "zhangsan ");
Map1.put ("user_ip", "192.168.0.1 ");
Map2.put ("user_name", "zhangsan ");
Map2.put ("user_ip", "192.168.0.2 ");
Map3.put ("user_name", "wangwu ");
Map3.put ("user_ip", "192.168.0.3 ");
List. add (map1 );
List. add (map2 );
List. add (map3 );
MyAdapter listAdapter = new MyAdapter (this,
List, R. layout. user, new String [] {"user_name", "user_ip "},
New int [] {R. id. user_name, R. id. user_ip });
/** This is the object of the current ListActivity,
* List keyword. Place the cursor over the list and you can see the ArrayList <HashMap <String, String> list
* Indicates: first, it is an ArrayList with HashMap and two String objects in HashMap.
* R. id. uder_name is the layout file, followed by an array, and the last long string is the control layout displayed in the list.
* User_name and user_ip correspond to columns in HashMap, and the last new int [] corresponds to the values in the column */
SetListAdapter (listAdapter );
}
}

Main. xml:
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <LinearLayout android: id = "@ + id/listLinearLayout" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: orientation = "vertical"> <ListView android: id = "@ id/android: list" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: drawselectid Top = "true" android: scrollbars = "vertical"/> </LinearLayout>
 
 
Uesr. xml:
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "horizontal" android: paddingLeft = "10dip" android: paddingRight = "10dip" android: paddingTop = "1dip" android: paddingBottom = "1dip"> <TextView android: id = "@ + id/user_name" android: layout_width = "180dip" android: layout_height = "30dip" android: textSize = "5pt" android: singleLine = "true"/> <TextView android: id = "@ + id/user_ip" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: gravity = "right" android: textSize = "5pt"/> </LinearLayout>

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.