使用RadioGroup步骤
1、在activity的xml文件中添加代码如下
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/radioGroup"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="0dp"
android:textSize="20dp"
android:id="@+id/A_mode"
android:text="开始绘图"/>
<RadioButton
android: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 code as follows:
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroup;//选择按钮
private static final String TAG="MainActivity";
@Override
protected 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() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int id) {
RadioButton radio =(RadioButton)findViewById(id) ;
if(radio.getText().equals("开始绘图"))
{
}
else//停止绘图
{
}
}
});
}
/*退出时取消绑定服务以及广播监听*/
@Override
protected void onDestroy() {
super.onDestroy();
}
}
Results as shown below: