안드로이드에서 스위치 버튼을 추가하는 방법

 

이 게시물에서는 Android에서 스위치 활성화 및 비활성화 버튼을 구현하는 방법을 보여줍니다.

 

1. Android Studio에서 프로젝트를 열고 build.gradle 파일에 아래의 종속성을 추가 한 다음 프로젝트를 동기화하십시오

implementation 'com.github.zcweng:switch-button:0.0.3@aar'

2. 그런 다음 XML 파일에 아래 코드를 추가하십시오.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">



    <com.suke.widget.SwitchButton
        android:id="@+id/sb"
        android:layout_width="100dp"
        android:layout_height="60dp"
        android:layout_marginLeft="30dp"
        android:layout_margin="10dp"
        android:tint="@color/colorAccent"
        android:layout_centerHorizontal="true"
        android:paddingRight="16dp"
        android:layout_gravity="center"
        app:sb_button_color="@color/white"
        app:sb_shadow_color="#A36F95"
        app:sb_background="#FFF"
        app:sb_checkline_color="@color/green"
        app:sb_checked_color="@color/green"
        app:sb_uncheck_color="@color/red"
        app:sb_uncheckcircle_color="@color/red"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

3. Java 클래스 파일에 아래 코드를 추가하십시오.

package com.jwlee.switch_test;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Toast;

import com.suke.widget.SwitchButton;

public class MainActivity extends AppCompatActivity {

    SwitchButton swithButton;

    SharedPreferences.Editor prefEditor;
    SharedPreferences prefs;

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

        swithButton = findViewById(R.id.sb);
        prefEditor = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
        prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

        swithButton.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {

                if (isChecked){

                    // DO what ever you want here when button is enabled
                    Toast.makeText(MainActivity.this, "Enabled", Toast.LENGTH_SHORT).show();
                    prefEditor.putString("checked","yes");
                    prefEditor.apply();

                }else {

                    // DO what ever you want here when button is disabled
                    Toast.makeText(MainActivity.this, "Disabled", Toast.LENGTH_SHORT).show();
                    prefEditor.putString("checked","false");
                    prefEditor.apply();
                }


            }
        });


        if (prefs.getString("checked","no").equals("yes")){

            swithButton.setChecked(true);

        }else {

            swithButton.setChecked(false);
        }
    }
}

4. color.xml 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
    <color name ="white">#ffffff</color>
    <color name ="red">#ff0000</color>
    <color name ="green">#00ff00</color>
</resources>

5. 더 나은 이해를 위해 아래 비디오 시청

 

+ Recent posts