Android Studio Development Note Four

Source: Internet
Author: User

CheckBox: check box
1. Two statuses: checked and unchecked
2. Properties:
Android:id Android:layout_width:android:layout_height:
Android:checked Android:text

Radiogroup and RadioButton
A collection of 1.radiogroup:radiobutton that provides a mechanism for multiple selection
2. Properties: android:orientation= "Vertical"--Vertical distribution
"Horizontal"--Horizontal distribution

Activitymain:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
tools:context= "Com.example.administrator.checkbox.MainActivity" >

<checkbox
Android:id= "@+id/checkbox1"
Android:checked= "false"
android:text= "Basketball"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>

<radiogroup
android:orientation= "Horizontal"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/radiogroup1"
>
<radiobutton
Android:id= "@+id/radiobutton1"
Android:checked= "false"
android:text= "Male"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<radiobutton
Android:id= "@+id/radiobutton2"
Android:checked= "false"
android:text= "Female"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
</RadioGroup>
</LinearLayout>

Mainactivity:
Package Com.example.administrator.checkbox;

Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.widget.CheckBox;
Import Android.widget.CompoundButton;
Import Android.widget.RadioGroup;

public class Mainactivity extends Appcompatactivity {
private checkbox checkbox;
Private Radiogroup RG;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initializing the control
checkbox= (CheckBox) Findviewbyid (r.id.checkbox1);
Rg= (Radiogroup) Findviewbyid (r.id.radiogroup1);
Sets the listener event to check if the checkbox is selected
Checkbox.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {
@Override
public void OnCheckedChanged (Compoundbutton buttonview, Boolean isChecked) {
Use the OnCheckedChanged method to listen to whether the current checkbox is selected
LOG.I ("tag", ischecked+ "");
if (isChecked) {
Get the text content of a checkbox
String Text=checkbox.gettext (). toString ();
LOG.I ("tag", text);
}
}
});
Implement Radiogroup Listener Events
Rg.setoncheckedchangelistener (New Radiogroup.oncheckedchangelistener () {
@Override
public void OnCheckedChanged (radiogroup group, int checkedid) {
Switch (checkedid) {
Case R.id.radiobutton1:
LOG.I ("tab", "You are a boy!");
Break
Case R.id.radiobutton2:
LOG.I ("tag", "You are a girl!");
Break
}
}
});
}
}

Android Studio Development Note Four

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.