-1

我正在尝试将添加文本更改侦听器添加到我的代码中,因此当我编辑文本时,它会自动将货币转换为选定的单选按钮(美元、欧元),而无需按下其中一个单选按钮使其显示在文本视图中回答。

我不知道该怎么做我是新的编程在谷歌搜索没有找到答案

package com.gardana.superh;

public class ConvertActivity extends Activity {
TextView mResult;
EditText mToConvert;
RadioGroup mRadioGroup;
RadioButton mDollar, Meuro;





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 mradRadioGroup, int checkedId)
        {

            switch (checkedId)
            {

            case R.id.dollar:
            Double dollarConvert = Double.valueOf(mToConvert.getText().toString()); //convert the string to int
            double price = dollarConvert * 3.76;
            mDollar.setChecked(true);
            Meuro.setChecked(false);
            mResult.setText("$"+price);

            break;

            case R.id.euro:

                Double euroConvert = Double.valueOf(mToConvert.getText().toString()); //convert the string to int
                double value = euroConvert * 5;
                mDollar.setChecked(false);
                Meuro.setChecked(true);

                mResult.setText("€" + value);
                Meuro.setChecked(false);

                break;

                default:;
            }
        }
    });



}

}

4

1 回答 1

1

我建议您分解代码以使其更容易执行。首先,您需要添加两个侦听器,以便在需要时执行转换。

mToConvert.addTextChangedListener(new TextWatcher() {

    @Override
    public void afterTextChanged(Editable s) {
        convertCurrentAmount();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {                
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {                
    }


});
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    public void onCheckedChanged(RadioGroup mradRadioGroup, int checkedId)
    {
        convertCurrentAmount();
    }
});

因为你基本上要在两个地方做同样的工作,我建议你创建一个为你执行计算的方法。然后,只要您想更新转换,就可以调用它。

   public void convertCurrentAmount() {
        int exchangeRate = -1;
        String exchangeSymbol;
        switch (mRadioGroup.getCheckedRadioButtonId()) {
            case R.id.dollar:
                exchangeRate = 3.76;
                exchangeSymbol = "$";
                break;

            case R.id.euro:
                exchangeRate = 5;
                exchangeSymbol = "€";
                break;
        }

        if (exchangeRate > 0 && exchangeSymbol != null) {
            // Perform Calculation
        }
    }

我还在上面提供了一个关于如何重新设计开关/案例以减少代码重复的建议,因为重复编写相同的东西是没有意义的。

于 2012-12-18T21:23:14.047 回答