๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’š ๊ฐœ๋ฐœ/์•ˆ๋“œ๋กœ์ด๋“œ

[์•ˆ๋“œ๋กœ์ด๋“œ] Switch ๋ฒ„ํŠผ ์‚ฌ์šฉ๋ฒ•

by devshin.kr 2021. 3. 22.
728x90

 

off
on

public class MainActivity extends AppCompatActivity {

    int nChecked = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Switch switchButton = findViewById(R.id.sw_opt);
        switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                	// switchButton์ด ์ฒดํฌ๋œ ๊ฒฝ์šฐ
                    nChecked = 1;
                } else {
                	// switchButton์ด ์ฒดํฌ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
                    nChecked = 0;
                }
            }
        });

    }
   ...

 

 

 

๋Œ“๊ธ€