maximum one item can be selected.
Here you also find how get selected radio button.
In this example i used AbsoluteLayout for better positioning.
//main.xml
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/myTextViev" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Choose Dictionary:" android:layout_x="20px" android:layout_y="250px" /> <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Rock" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Scissors" /> <RadioButton android:id="@+id/radio3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Paper" /> </RadioGroup> <Button android:id="@+id/myButton" android:layout_width="100px" android:layout_height="50px" android:text="Submit" android:layout_x="5px" android:layout_y="60px" /> </AbsoluteLayout>
//AndroidRadioButton.java
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.TextView; public class AndroidRadioButton extends Activity implements OnClickListener{ TextView tv; Button btn; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button) findViewById(R.id.myButton); btn.setOnClickListener(this); tv = (TextView) findViewById(R.id.myTextViev); tv.setText("This is sample text"); } @Override public void onClick(View v) { // TODO Auto-generated method stub RadioButton ra=(RadioButton)findViewById(R.id.radio1); RadioButton rb=(RadioButton)findViewById(R.id.radio2); RadioButton rc=(RadioButton)findViewById(R.id.radio3); if (v.getId() == R.id.myButton) { if(ra.isChecked()){ tv.setText("Rock selected"); } if(rb.isChecked()){ tv.setText("Scissors selected"); } if(rc.isChecked()){ tv.setText("Paper selected"); } } } }
output:
select any item of radio button and then click submit button (let i select Scissors radio button),
now output will be
This comment has been removed by the author.
ReplyDelete