Android Radio Button

I have a switch group on Android that looks something like this:

Choose a color:

  • Red
  • blue
  • Orange
  • Green

I need to get the selected switch, as well as its value.

I have 4 radio objects in this way in the radio group rg

 rb1a=(RadioButton)findViewById(R.id.rb1a); rb1b=(RadioButton)findViewById(R.id.rb1b); rb1c=(RadioButton)findViewById(R.id.rb1c); rb1d=(RadioButton)findViewById(R.id.rb1d); tv1=(TextView)findViewById(R.id.tv1); next1=(Button)findViewById(R.id.next1); rg=(RadioGroup)findViewById(R.id.rg); // I have error after this line.please help rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { } @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub } }); 
+6
android radio-button
source share
2 answers

You can test the radion button using the isChecked() function.

for ex:

 if(radio1_red.isChecked()) { txtView.setText("Red button is checked"); } 

Take a look at the Example .

You can also link to this Page - Form Stuff listed on android-sdk pages.

Do this to get the selected switch, as well as its value:

  private OnClickListener radio_listener = new OnClickListener() { public void onClick(View v) { // Perform action on clicks RadioButton rb = (RadioButton) v; Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show(); } }; 
+5
source share

as you can see in the Android documentation http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html OnCheckedChangeListener has only the onCheckedChanged method (RadioGroup group, int checkedId) and does not contain the public void onCheckedChangedound method arg0, boolean arg1) β†’ delete it and try again.

Here you will find an example: http://developer.android.com/guide/tutorials/views/hello-formstuff.html

considers

+4
source share

All Articles