1
+ package com .example .lenovo .radiobutton ;
2
+
3
+ import android .support .v7 .app .AppCompatActivity ;
4
+ import android .os .Bundle ;
5
+ import android .app .Activity ;
6
+ import android .view .View ;
7
+ import android .view .View .OnClickListener ;
8
+ import android .widget .Button ;
9
+ import android .widget .RadioButton ;
10
+ import android .widget .RadioGroup ;
11
+ import android .widget .Toast ;
12
+
13
+ public class radiobutton extends Activity {
14
+
15
+ private RadioGroup radiogroup ;
16
+ private RadioButton radiobutton ;
17
+ private Button btnDisplay ;
18
+
19
+ @ Override
20
+ public void onCreate (Bundle savedInstanceState ) {
21
+ super .onCreate (savedInstanceState );
22
+ setContentView (R .layout .activity_radiobutton );
23
+
24
+ addListenerOnButton ();
25
+
26
+ }
27
+
28
+ public void addListenerOnButton () {
29
+
30
+ radiogroup = (RadioGroup ) findViewById (R .id .radioquestion );
31
+ btnDisplay = (Button ) findViewById (R .id .btnDisplay );
32
+
33
+ btnDisplay .setOnClickListener (new OnClickListener () {
34
+
35
+ @ Override
36
+ public void onClick (View v ) {
37
+
38
+ // get selected radio button from radioGroup
39
+ int selectedId = radiogroup .getCheckedRadioButtonId ();
40
+
41
+ // find the radiobutton by returned id
42
+ radiobutton = (RadioButton ) findViewById (selectedId );
43
+
44
+ Toast .makeText (radiobutton .this ,
45
+ radiobutton .getText (), Toast .LENGTH_SHORT ).show ();
46
+
47
+ }
48
+
49
+ });
50
+
51
+ }
52
+ }
0 commit comments