使用RadioGroup步骤1、在activity的xml文件中添加代码如下
<RadioGroupandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/radioGroup"android:orientation="horizontal"><RadioButtonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_marginRight="0dp"android:textSize="20dp"android:id="@+id/A_mode"android:text="开始绘图"/><RadioButtonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_marginRight="10dp"android:textSize="20dp"android:id="@+id/M_mode"android:text="停止绘图"/></RadioGroup>
activity中代码如下:
public class MainActivity extends AppCompatActivity {private RadioGroup radioGroup;//选择按钮private static final String TAG="MainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);/*控制绘图开始与停止*/radioGroup = (RadioGroup)findViewById(R.id.radioGroup);radioGroup.check(R.id.A_mode);//切换为开始绘图radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup radioGroup, int id) {RadioButton radio =(RadioButton)findViewById(id) ;if(radio.getText().equals("开始绘图")){ }else//停止绘图{ }}});}/*退出时取消绑定服务以及广播监听*/@Overrideprotected void onDestroy() {super.onDestroy();}
}
效果如下图所示: