使用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中代碼如下:
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();
}
}
效果如下圖所示: