Switch Doubt

0

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

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();
}
    
asked by anonymous 11.03.2015 / 20:26

1 answer

2

I'm not sure I can understand your question, but if you want to identify the Switch on / off actions, use OnCheckedChangeListener for this:

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