我在RadioGroup Listener上遇到问题,我对android有点不熟悉,希望您的帮助。
我有5个单选按钮,每个按钮都有不同的值。
这是以下关于Java的代码
package com.com.calculartmb;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity {
// variaveis
double altura;
double peso;
double resultado;
double nivel;
EditText pesoEd;
EditText alturaEd;
EditText finalEd;
SeekBar altSeekBar;
SeekBar pesoSeekBar;
RadioButton radio1;
RadioButton radio2;
RadioButton radio3;
RadioButton radio4;
RadioButton radio5;
RadioButton radioGrupo;
private double[] checklistValues = new double[6];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// iniciação
pesoEd = (EditText) findViewById(R.id.editText1);
alturaEd = (EditText) findViewById(R.id.editText2);
finalEd = (EditText) findViewById(R.id.editText3);
altSeekBar = (SeekBar) findViewById(R.id.seekBar1);
pesoSeekBar = (SeekBar) findViewById(R.id.seekBar2);
radio1 = (RadioButton) findViewById(R.id.radio1);
radio2 = (RadioButton) findViewById(R.id.radio2);
radio3 = (RadioButton) findViewById(R.id.radio3);
radio4 = (RadioButton) findViewById(R.id.radio4);
radio5 = (RadioButton) findViewById(R.id.radio5);
radioGrupo = (RadioButton) findViewById(R.id.radioGroup1);
altSeekBar.setMax(25000);
pesoSeekBar.setMax(60000);
//listeners
addChangeListenerToRadios();
altSeekBar.setOnSeekBarChangeListener(altSeekBarListener);
pesoSeekBar.setOnSeekBarChangeListener(pesoSeekBarListener);
}
private OnSeekBarChangeListener pesoSeekBarListener = new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
// Calcula o novo valor do TIP
peso = (pesoSeekBar.getProgress()) * .01;
// mostra na caixa o valor novo
pesoEd.setText(String.format("%.02f", peso).replace(',', '.'));
// Chama o update
updateValorTMB();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
};
private OnSeekBarChangeListener altSeekBarListener = new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
// Calcula o novo valor do TIP
altura = (altSeekBar.getProgress()) * .01;
// mostra na caixa o valor novo
alturaEd.setText(String.format("%.02f", altura).replace(',', '.'));
// Chama o update
updateValorTMB();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
};
private void updateValorTMB() {
double peso = Double.parseDouble(pesoEd.getText().toString());
double altura = Double.parseDouble(alturaEd.getText().toString());
double resultado = 655 + (9.6 * peso) + (1.8 * altura); //- (4.7 * nivel);
finalEd.setText(String.format("%.02f", resultado));
}
public void addChangeListenerToRadios(){
radioGrupo.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
checklistValues[0] = (radio1.isChecked())?1.2:0;
checklistValues[1] = (radio2.isChecked())?1.37:0;
checklistValues[2] = (radio3.isChecked())?1.65:0;
checklistValues[3] = (radio4.isChecked())?1.72:0;
checklistValues[4] = (radio5.isChecked())?1.9:0;
setNivelFromChecklist();
updateValorTMB();
}
});
}
private void setNivelFromChecklist(){
double total= 0;
for (double item:checklistValues){
total += item;
}
nivel = total * .01;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
更新:现在出现新的错误消息!
最佳答案
radioGrupo
的类型应更改为RadioGroup
(RadioButton radioGrupo;
-> RadioGroup radioGrupo;
和radioGrupo = (RadioButton) findViewById(R.id.radioGroup1);
-> radioGrupo = (RadioGroup) findViewById(R.id.radioGroup1);
),然后您将可以使用以下代码:
radioGrupo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
}
});