本帖最后由 liu 于 2018-12-6 15:38 编辑
RadioGroup和RadioButton的简单用法
Android新手开发之旅【目录】
RadioGroup是单选组合框,可以容纳多个RadioButton,RadioGroup中包含的多个RadioButton只能选择一个
RadioGroup常用属性:
RadioGroup.getCheckedRadioButtonId() 获取选中按钮的id
RadioGroup.clearCheck() 清除选中状态
RadioButton.getText() 获取单选框的值
RadioGroup.setOnCheckedChangeListener (RadioGroup.OnCheckedChangeListener listener) 监听事件
示例代码:
activity_main:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择城市:"
android:textColor="@android:color/black"
android:textSize="18sp" />
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp">
<RadioButton
android:id="@+id/radiobutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开封" />
<RadioButton
android:id="@+id/radiobutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="郑州" />
<RadioButton
android:id="@+id/radiobutton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="北京" />
</RadioGroup>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="你选择的是:"
android:textColor="@android:color/black"
android:textSize="18sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="清除选中状态"
android:textColor="@android:color/black" />
</LinearLayout>
java代码:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private RadioGroup mRadioGroup;
private RadioButton mRadioButton1;
private RadioButton mRadioButton2;
private RadioButton mRadioButton3;
private TextView mTextView;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRadioGroup = (RadioGroup) findViewById(R.id.radiogroup);
mRadioButton1 = (RadioButton) findViewById(R.id.radiobutton1);
mRadioButton2 = (RadioButton) findViewById(R.id.radiobutton2);
mRadioButton3 = (RadioButton) findViewById(R.id.radiobutton3);
mTextView = (TextView) findViewById(R.id.textview);
mButton = (Button) findViewById(R.id.button);
//RadioGroup监听事件
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
//radioGroup.getCheckedRadioButtonId()获取状态改变的RadioButton的id
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.radiobutton1:
mTextView.setText("你选择的是: " + mRadioButton1.getText().toString());
break;
case R.id.radiobutton2:
mTextView.setText("你选择的是: " + mRadioButton2.getText().toString());
break;
case R.id.radiobutton3:
mTextView.setText("你选择的是: " + mRadioButton3.getText().toString());
break;
}
}
});
//清除选中状态
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mRadioGroup.clearCheck();
}
});
}
}
效果如下图:
|