Created
August 14, 2012 15:33
-
-
Save benwilson512/3350310 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.button.radio; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.RadioButton; | |
import android.widget.RadioGroup; | |
import android.widget.Toast; | |
public class Selector extends Activity { | |
private RadioGroup radioGroup; | |
private RadioButton radioSexButton; | |
private Button btnDisplay; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
radioGroup = (RadioGroup) findViewById(R.id.radioGroup); | |
btnDisplay = (Button) findViewById(R.id.btnDisplay); | |
btnDisplay.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// THESE ARE THE MOST IMPORTANT LINES | |
int selectedId = radioGroup.getCheckedRadioButtonId(); | |
radioSexButton = (RadioButton) findViewById(selectedId); | |
// END MOST IMPORTANT LINES | |
Toast.makeText(Selector.this, radioSexButton.getText(), Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
} | |
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
> | |
<TextView | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:text="@string/hello" | |
/> | |
<RadioGroup | |
android:id="@+id/radioGroup" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" > | |
<RadioButton | |
android:id="@+id/radioMale" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/radio_male" | |
android:checked="true" /> | |
<RadioButton | |
android:id="@+id/radioFemale" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/radio_female" /> | |
<RadioButton | |
android:id="@+id/radioFemale" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/radio_forget" /> | |
</RadioGroup> | |
<Button | |
android:id="@+id/btnDisplay" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/btn_display" /> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feel free to use radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { }); instead of the button and its listener.