Я предполагаю, что вы знаете, как создать макет, привязать его к деятельности и получить взгляды с findViewById
. Если вы не просто Google это
Достижение эффекта, который вы хотите, это довольно легко. Вот как вы это делаете:
Вот минимальный рабочий пример:
public class MainActivity extends AppCompatActivity {
Switch testSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get the switch from layout
testSwitch = (Switch) findViewById(R.id.test_switch);
//set text
testSwitch.setText("Example switch");
//set on checked change listener
testSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@
Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
//set switch text based on checked status
if (isChecked) {
compoundButton.setText("Turning on");
} else {
compoundButton.setText("Turning off");
}
//disable it
compoundButton.setEnabled(false);
//start the big long process (activate bluetooth or wifi). That's only an example
afterSomeTime();
}
});
}
public void afterSomeTime() {
//After 5 seconds update the switch
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
testSwitch.setEnabled(true);
if (testSwitch.isChecked()) {
testSwitch.setText("ON");
} else {
testSwitch.setText("OFF");
}
}
}, 5000);
}
}
Надеюсь, это поможет немного