Doubt of Switch

Asked

Viewed 123 times

0

I have a dialog with Switch and programmed with Click to change and now I need to do the same thing when the user drags the Switch off to on or the other way.

public void Dialogo_Setor() {
    dialog.setTitle("Setores");
    final Switch comum = (Switch) dialog.findViewById(R.id.switchAreaComun);
    final Switch externa = (Switch) dialog.findViewById(R.id.switchAreaExterna);
    final Switch intima = (Switch) dialog.findViewById(R.id.switchAreaIntima);


    comum.setChecked(setorcomum);
    externa.setChecked(setorexterna);
    intima.setChecked(setorintima);
    final Handler handler = new Handler();

    comum.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            doCommand((comum.isChecked() ? "arme" : "desarme") + " área comum", false);
            comum.setChecked(setorcomum);
            final Runnable r = new Runnable() {
                public void run() {
                    if (comum.isChecked()!= setorcomum){
                        comum.setChecked(setorcomum);
                        handler.removeCallbacksAndMessages(null);
                    }else {
                        handler.postDelayed(this, 1000);
                    }
                }
            };
            handler.postDelayed(r, 1000);
        }
    });


    externa.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            doCommand((externa.isChecked() ? "arme" : "desarme") + " área externa", false);
            externa.setChecked(setorexterna);
            final Runnable r = new Runnable() {
                public void run() {
                    if (externa.isChecked()!= setorexterna){
                        externa.setChecked(setorexterna);
                        handler.removeCallbacksAndMessages(null);
                    }else {
                        handler.postDelayed(this, 1000);
                    }
                }
            };
            handler.postDelayed(r, 1000);
        }
    });



    intima.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            doCommand((intima.isChecked() ? "arme" : "desarme") + " área íntima", false);
            intima.setChecked(setorintima);
            final Runnable r = new Runnable() {
                public void run() {
                    if (intima.isChecked()!= setorintima){
                        intima.setChecked(setorintima);
                        handler.removeCallbacksAndMessages(null);
                    }else {
                        handler.postDelayed(this, 1000);
                    }
                }
            };
            handler.postDelayed(r, 1000);
        }
    });

    //  global.setFotobitmap(null);
    dialog.show();
}

1 answer

2

I’m not sure if I understand your doubt, but if you want to identify the on/off actions of Switch, use the OnCheckedChangeListener for that reason:

comum.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // Ligado
        } else {
            // Desligado
        }
    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.