Android |单选按钮不会做任何事情并保持按下状态

时间:2012-12-18 15:18:32

标签: java android radio-button

我试图让货币转换器做作业(我是编程新手)

制作了所有东西,但单选按钮被按下并且什么都不做 (不将文本放入TextView,单选按钮在“按下”模式下被锁定

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_convert);

    mResult = (TextView) findViewById(R.id.result);
    mToConvert = (EditText) findViewById(R.id.toConvert);
    mRadioGroup = (RadioGroup) findViewById(R.id.radioG);
    mDollar = (RadioButton) findViewById(R.id.dollar);
    Meuro = (RadioButton) findViewById(R.id.euro);

    mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup rGroup, int checkedId)
        {

            switch (mRadioGroup.getCheckedRadioButtonId())
            {
            case R.id.dollar:
            Double dollarConvert = Double.valueOf(mToConvert.getText().toString()); //convert the string to int
            double price = dollarConvert * 1.28;
            mDollar.setChecked(true);
            Meuro.setChecked(false);
            String result = mToConvert.getText().toString();
            mResult.setText(result + price);
            break;

            case R.id.euro:
                Double euroConvert = Double.valueOf(mToConvert.getText().toString()); //convert the string to int
                double value = euroConvert * 1.28;
                mDollar.setChecked(false);
                Meuro.setChecked(true);
                String result1 = mToConvert.getText().toString();
                mResult.setText(result1 + value);
                break;

                default:;
            }
        }
    });


}

}

1 个答案:

答案 0 :(得分:1)

当检查更改事件触发时,您需要为RadioGroup设置RadioGroup.setoncheckedchangelistener以执行某些操作。

mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
    public void onCheckedChanged(RadioGroup rGroup, int checkedId)
    {
              //do your code here
    }
});