1.switch用法
aSwitch = findViewById(R.id.switch1);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
//开关打开时的操作
}else {
//开关关闭时的操作
}
}
});
aSwitch.setText("设置switch前面显示的文字");
2.radioGroup、radioButton用法
radioGroup = findViewById(R.id.radioGroup);
radioButton1 = findViewById(R.id.radioButton);
radioButton2 = findViewById(R.id.radioButton2);
radioButton3 = findViewById(R.id.radioButton3);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radioButton:
//选中radioButton时进行的操作,radiobutton是控件在xml文件中的id
break;
case R.id.radioButton2:
break;
case R.id.radioButton3:
break;
}
}
});
3.radiobutton和switch结合
java
aSwitch = findViewById(R.id.switch1);
radioGroup = findViewById(R.id.radioGroup);
radioButton1 = findViewById(R.id.radioButton);
radioButton2 = findViewById(R.id.radioButton2);
radioButton3 = findViewById(R.id.radioButton3);
textView = findViewById(R.id.textView);
textView.setText("-1");
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radioButton:
//选中radioButton时进行的操作,radiobutton是控件在xml文件中的id
textView.setText("-1");
break;
case R.id.radioButton2:
textView.setText("-2");
break;
case R.id.radioButton3:
textView.setText("-3");
break;
}
}
});
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radioButton:
textView.setText("1");
break;
case R.id.radioButton2:
textView.setText("2");
break;
case R.id.radioButton3:
textView.setText("3");
break;
}
}
});
}else{
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radioButton:
textView.setText("-1");
break;
case R.id.radioButton2:
textView.setText("-2");
break;
case R.id.radioButton3:
textView.setText("-3");
break;
}
}
});
}
int buttonId =radioGroup.getCheckedRadioButtonId();
radioGroup.clearCheck();
radioGroup.check(buttonId);
}
});
xml
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Switch"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="52dp"
android:layout_marginBottom="75dp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/horizontalScrollView">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton"
android:checked="true"/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="116dp"
android:layout_marginBottom="97dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
效果图