'Radio buttons in android studio
I want to have list of radio buttons and after i select one and click next I would like to perform different actions depending on which button was pressed. i am trying currently to set text for some test TextView and for every radio button it says it is -1.
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.*;
import android.widget.RadioGroup;
import android.widget.TextView;
public class Polynomials extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View polyView = inflater.inflate(R.layout.fragment_poly, container, false);
//layout
RadioGroup rbg = (RadioGroup) polyView.findViewById(R.id.rBGPoly);
Button next = (Button) polyView.findViewById(R.id.polyButton);
final TextView test = (TextView) polyView.findViewById(R.id.jdtTest);
final RadioButton but1 = (RadioButton) polyView.findViewById(R.id.radioButton1);
RadioButton but2 = (RadioButton) polyView.findViewById(R.id.radioButton2);
RadioButton but3 = (RadioButton) polyView.findViewById(R.id.radioButton3);
RadioButton but4 = (RadioButton) polyView.findViewById(R.id.radioButton4);
RadioButton but5 = (RadioButton) polyView.findViewById(R.id.radioButton5);
RadioButton but6 = (RadioButton) polyView.findViewById(R.id.radioButton6);
RadioButton but7 = (RadioButton) polyView.findViewById(R.id.radioButton7);
RadioGroup g = (RadioGroup)polyView.findViewById(R.id.rBGPoly);
final int selected = g.getCheckedRadioButtonId();
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
test.setText(""+selected);
}
});
return polyView;
}
}
and this is my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/jdttae"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/jdtchoose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:text="How many terms?"
android:textSize="18sp" />
<RadioGroup android:id="@+id/rBGPoly"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginLeft="180dp"
android:text="1"
android:layout_below="@+id/jdtchoose"
android:layout_alignLeft="@+id/jdtchoose"
/>/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_marginLeft="180dp"
android:layout_marginTop="31dp"
android:layout_below="@+id/radioButton1"
android:layout_alignLeft="@+id/radioButton1"
/>/>
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_marginLeft="180dp"
android:layout_marginTop="31dp"
android:layout_below="@+id/radioButton2"
android:layout_alignLeft="@+id/radioButton2"
/>/>
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
android:layout_marginLeft="180dp"
android:text="4"
android:layout_below="@+id/radioButton3"
android:layout_alignLeft="@+id/radioButton3"
/>/>
<RadioButton
android:id="@+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
android:layout_marginLeft="180dp"
android:text="5"
android:layout_below="@+id/radioButton4"
android:layout_alignLeft="@+id/radioButton4"
/>/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:layout_marginLeft="180dp"
android:id="@+id/radioButton6"
android:layout_below="@+id/radioButton5"
android:layout_alignLeft="@+id/radioButton5"
android:layout_marginTop="31dp"
/>/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:layout_marginLeft="180dp"
android:id="@+id/radioButton7"
android:layout_below="@+id/radioButton6"
android:layout_alignParentBottom="false"
android:layout_alignLeft="@+id/radioButton6"
android:layout_marginTop="31dp"
android:checked="false"
/>/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="@+id/polyButton"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/jdtchoose"
android:layout_marginBottom="26dp"
android:layout_alignRight="@+id/jdtchoose" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:id="@+id/jdtTest"
android:editable="false"
android:textSize="18sp"
android:layout_alignBaseline="@+id/polyButton"
android:layout_alignBottom="@+id/polyButton"
android:layout_toLeftOf="@+id/polyButton"
android:layout_marginRight="47dp" />
</RelativeLayout>
Solution 1:[1]
call g.getCheckedRadioButtonId
on next Button click which return currently selected rado button id in RadioGroup:
public void onClick(View v) {
int selected = g.getCheckedRadioButtonId();
test.setText(String.valueof(selected));
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ÏÑσѕÏÑ”Ñ K |