This example describes the Radiogroup usage of Android programming. Share to everyone for your reference, specific as follows:
Radiogroup is sometimes more useful. The main feature is to provide users with a more selective mechanism.
Mainactivity.java
Package Com.example.lesson16_radio;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.RadioButton;
Import Android.widget.RadioGroup;
Import Android.widget.Toast;
public class Mainactivity extends activity {private Radiogroup Group_temo;
Private RadioButton Checkradiobutton;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Group_temo = (radiogroup) Findviewbyid (r.id.radiogroup1);
Change the default option Group_temo.check (R.ID.RADIO1);
Gets the default selected value Checkradiobutton = (RadioButton) Group_temo.findviewbyid (Group_temo. Getcheckedradiobuttonid ());
Toast.maketext (this, the value of the default option is: "+ checkradiobutton.gettext (), Toast.length_long). Show (); Registering event Group_temo. Setoncheckedchangelistener (New Radiogroup.oncheckedchangelistener () {@Override public void OnCheckedChanged (radiogroup group, int checkedid) {//Click event to get the Selection object Checkradiobutton = (RAdiobutton) Group_temo. Findviewbyid (Checkedid);
Toast.maketext (Getapplicationcontext (), "Get ID is" + checkradiobutton.gettext (), Toast.length_long). Show ();
}
});
}
}
Layout file
<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/radiogroup1 "android:layout_width=" Wrap_content "Android:layout_" height= "Wrap_content" android:layout_alignparentleft= "true" android:layout_alignparentright= "true" android:layout _alignparenttop= "true" > <radiobutton android:id= "@+id/radio0" android:layout_width= "Wrap_content" Androi d:layout_height= "Wrap_content" android:checked= "true" android:text= "@string/text_java"/> <radiobutton an Droid:id= "@+id/radio1" android:layout_width= "Wrap_content" Android:layout_height= "Wrap_content" android:text= "@string/text_net"/> <radiobutton android:id= "@+id/radio2" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/text_php"/>
;/radiogroup> </RelativeLayout>
I hope this article will help you with your Android programming.