Evento CheckBox |
Top Previous Next |
|
Tela
Modo 1 - criando eventos anônimos
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView;
public class SomaChecksActivity extends Activity {
CheckBox chkBebida, chkChips, chkAcompanha; TextView tvResultado;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
chkBebida = (CheckBox) findViewById(R.id.chkBebida); chkChips = (CheckBox) findViewById(R.id.chkChips); chkAcompanha = (CheckBox) findViewById(R.id.chkAcompanha); tvResultado = (TextView) findViewById(R.id.tvResultado);
chkBebida .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Somar();
} });
chkChips.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Somar();
} });
chkAcompanha .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Somar();
} }); }
public void Somar() { double total = 0; if (chkBebida.isChecked()) total += 2.0; if (chkChips.isChecked()) total += 1.5; if (chkAcompanha.isChecked()) total += 3.0;
tvResultado.setText("Total R$ " + String.format("%1$.2f", total)); }
}
Modo 2 - implementando a Interface
import android.app.Activity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView;
public class SomaChecksActivity extends Activity implements CompoundButton.OnCheckedChangeListener {
CheckBox chkBebida, chkChips, chkAcompanha; TextView tvResultado;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
chkBebida = (CheckBox) findViewById(R.id.chkBebida); chkChips = (CheckBox) findViewById(R.id.chkChips); chkAcompanha = (CheckBox) findViewById(R.id.chkAcompanha); tvResultado = (TextView) findViewById(R.id.tvResultado);
chkBebida.setOnCheckedChangeListener(this); chkChips.setOnCheckedChangeListener(this); chkAcompanha.setOnCheckedChangeListener(this); }
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { double total = 0; if (chkBebida.isChecked()) total += 2.0; if (chkChips.isChecked()) total += 1.5; if (chkAcompanha.isChecked()) total += 3.0;
tvResultado.setText("Total R$ " + String.format("%1$.2f", total)); } }
Modo 3 - implementando objeto
import android.app.Activity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.TextView;
public class SomaChecksActivity extends Activity {
CheckBox chkBebida, chkChips, chkAcompanha; TextView tvResultado;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
chkBebida = (CheckBox) findViewById(R.id.chkBebida); chkChips = (CheckBox) findViewById(R.id.chkChips); chkAcompanha = (CheckBox) findViewById(R.id.chkAcompanha); tvResultado = (TextView) findViewById(R.id.tvResultado);
OnCheckedChangeListener evento = new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { double total = 0; if (chkBebida.isChecked()) total += 2.0; if (chkChips.isChecked()) total += 1.5; if (chkAcompanha.isChecked()) total += 3.0;
tvResultado.setText("Total R$ " + String.format("%1$.2f", total)); } };
chkBebida.setOnCheckedChangeListener(evento); chkChips.setOnCheckedChangeListener(evento); chkAcompanha.setOnCheckedChangeListener(evento); } } |