我是android的新手,我有一个按钮,当它点击它时会从EditText获取double值并在TextView中显示它。
我还有两个单选按钮,一个是positive
,另一个是negative
,我想当用户切换negative
按钮将双倍乘以-1时,我的代码是像:
public void buttonClicked(View view) {
try {
myDouble = Double.parseDouble((myDoubleEditText.getText().toString()));
} catch (NumberFormatException e) {
Toast.makeText(getApplicationContext(), "Enter a valid Double", LENGTH_LONG).show();
}
myTextView.setText("My Double is",""+myDouble)
}
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.positive:
if (checked)
myDouble = myDouble;
break;
case R.id.negative:
if (checked)
myDouble = myDouble * -1;
break;
}
}
}
请指导我这样做。
由于
答案 0 :(得分:0)
我认为更好的方法是使用RadioGroup并在其上设置监听器以相应地更改和更新View(保存您有2个或3个或4个等侦听器)。
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
switch(checkedId) {
case R.id.positive:
myDouble = myDouble;
break;
case R.id.negative:
myDouble = myDouble * -1;
break;
}
}
});