gpt4 book ai didi

java - RadioGroup 监听器显示错误

转载 作者:行者123 更新时间:2023-12-01 13:18:41 27 4
gpt4 key购买 nike

我在 RadioGroup Listener 上遇到问题,我对 Android 有点菜鸟,非常感谢您的帮助。

我有 5 个单选按钮,每个按钮都有不同的值。

enter image description here

下面是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) {

}
});

关于java - RadioGroup 监听器显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22233265/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com