RadioGroup

来源:互联网 发布:买水果的软件 编辑:程序博客网 时间:2024/05/22 12:00
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {    private RadioGroup radioGroup;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        radioGroup = (RadioGroup) findViewById(R.id.radio);        radioGroup.setOnCheckedChangeListener(this);    }    @Override    public void onCheckedChanged(RadioGroup group, int checkedId) {        switch (checkedId){            case R.id.radioButton:                Toast.makeText(this, "Man is checked", Toast.LENGTH_SHORT).show();                break;            case R.id.radioButton2:                Toast.makeText(this, "Woman is checked", Toast.LENGTH_SHORT).show();                break;            default:                break;        }    }}

<RadioGroup    android:id="@+id/radio"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="horizontal">    <RadioButton        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true"        android:text="man"        android:id="@+id/radioButton"/>    <RadioButton        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="woman"        android:id="@+id/radioButton2"/></RadioGroup>
0 0
原创粉丝点击