radio button radiogroup with check box checkbox

Source: Internet
Author: User

In Androidapp applications, radio buttons and checkboxes are also used frequently, so let's study them together. We need to learn the basic controls in Android: (1) radio button Radiogroup, (2) check box checkbox.


First, the Design login window

Open the "res/layout/activity_main.xml" file.

1. Drag the 1 radio button list Radiogroup (note automatically contains 3 radio buttons radiobutton), 2 checkboxes, and one button buttons from the toolbar to the activity. All 3 controls come from the form Widgets.

2. Open the activity_main.xml file.

We have modified the auto-generated code to the following code, specifically:

(1) The ID of Ratiogroup is modified to gender, and the IDs of two RadioButton are changed to male and female respectively, and their text is changed to male and female respectively;

Note: The 1th radio button android:checked= "True" indicates that this radio button is selected by default.

(2) The IDs of the two checkboxes are modified to football and basketball, and their texts are modified to be football and blue balls respectively;

(3) Buttion's ID is modified to save and its text is modified to "save".

<relativelayout 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= ". Mainactivity ">


<radiogroup
Android:id= "@+id/gender"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparentleft= "true"
Android:layout_alignparenttop= "true" >


<radiobutton
Android:id= "@+id/male"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:checked= "true"
android:text= "@string/male"/>


<radiobutton
Android:id= "@+id/female"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/female"/>
</RadioGroup>


<checkbox
Android:id= "@+id/football"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_alignleft= "@+id/gender"
android:layout_below= "@+id/gender"
android:text= "@string/football"/>


<checkbox
Android:id= "@+id/basketball"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_alignleft= "@+id/football"
android:layout_below= "@+id/football"
android:text= "@string/basketball"/>


<button
Android:id= "@+id/save"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_alignright= "@+id/basketball"
android:layout_below= "@+id/basketball"
android:layout_margintop= "28DP"
android:text= "@string/save"/>


</RelativeLayout>



Second, click event

Open "src/com.genwoxue.radiogroupcheckbox/ Mainactivity.java " file.

Then enter the following code:

Package COM.EXAMPLE.HW;


import android.app.Activity;
import Android.os.Bundle;
import Android.view.Menu;
import Android.view.View;
import Android.view.View.OnClickListener;
import Android.widget.Button;
Import Android.widget.CheckBox;
import Android.widget.RadioButton;
import Android.widget.Toast;


Public class Mainactivity extends Activity {
Private RadioButton Rbmale = null;
Private RadioButton Rbfemale = null;
Private CheckBox cbfootball = null;
Private CheckBox cbbasketball = null;
Private Button btnsave = null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
Rbmale = (RadioButton) Super.findviewbyid (R.id.male);
Rbfemale = (RadioButton) Super.findviewbyid (R.id.female);
Cbfootball = (CheckBox) Super.findviewbyid (R.id.football);
Cbbasketball = (CheckBox) Super.findviewbyid (R.id.basketball);
Btnsave = (button) Super.findviewbyid (r.id.save);//The button in the parentheses is written as a checkbox, resulting in classcastexception


//And emulator appears unfortunately,project name has stopped error
Btnsave.setonclicklistener (New Saveonclicklistener ());
}


@Override
Public Boolean Oncreateoptionsmenu (Menu menu) {
//inflate the menu; This adds items to the action bar if it is present.
getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
Private class Saveonclicklistener implements onclicklistener{

Public void OnClick (View v) {
String Sgender = "";
String sfav = "";
String sinfo = "";
if (rbmale.ischecked ()) {
Sgender = Rbmale.gettext (). toString ();
}
if (rbfemale.ischecked ()) {
Sgender = Rbfemale.gettext (). toString ();
}
if (cbfootball.ischecked ()) {
Sfav = Sfav+cbfootball.gettext (). toString ();
}
if (cbbasketball.ischecked ()) {
Sfav = Sfav+cbbasketball.gettext (). toString ();
}
Sinfo = "Gender:" +sgender+ "hobby:" +SFAV;
Toast.maketext (Getapplicationcontext (), Sinfo, Toast.length_short). Show ();
}


}
}


In the above code, we focus on the green section, the other is the simplest basic code, if you do not understand, please refer to the previous chapter content.

1. Section ①

Import 2 packages related to RadioButton, checkbox.

2. Section ②

Declares a 5 control variable.

3. Section ③

Similar to the previous chapter.

(1) The Findviewbyid () method completes the capture of 5 controls.

(2) "Save" button to add click Listen Event: Btnsave.setonclicklistener (New Saveonclicklistener ()).

4. Section ④

We create a new class Saveonclicklistener inheritance interface Onclicklistener to enable click event Snooping.

Toast.maketext (Getapplicationcontext (), Sinfo,toast.length_short). Show () to display prompt information: gender and hobby.

  Note: the isChecked () method is used to determine whether the RadioButton and CheckBox controls are selected and returns True if checked, otherwise flase is returned.




This article is from the "No Water Fish" blog, please be sure to keep this source http://javaqun.blog.51cto.com/10687700/1705338

radio button radiogroup with check box checkbox

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.