如何在android上使用多个广播组

时间:2011-09-11 01:47:53

标签: android radio-group

我正在开发我的第一个非常基本的Android应用程序。我已经做了一些基本的教程并设法启动并运行了一个基本的计算器,但我试图添加一个额外的无线电组,该应用程序不再工作。我一定做错了什么但不知道是什么! 请告诉我任何提示,谢谢! 这是我现在正在使用的完整java代码

package com.test.calculator;

import com.test.calculator.R;

import android.app.Activity;
import android.os.Bundle;

import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity {
    // Widgets 
    private EditText txtContainers;
    private EditText txtVolume;
    private EditText txtABV;
    private EditText txtCost;

    private RadioGroup rdoGroup1;
    private RadioGroup rdoGroup2;

    private Button btnCalculate;
    private Button btnClear;

    private TextView txtTotalVolume;
    private TextView txtTotalAlcohol;
    private TextView txtDrinkSize;
    private TextView txtNumberofDrinks;
    private TextView txtCostperDrink;

    private int radioCheckedId = -1;

    // Called on creation
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Eula.show(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        txtContainers = (EditText) findViewById(R.id.txtContainers);
        txtContainers.requestFocus();

        rdoGroup1 = (RadioGroup) findViewById(R.id.RadioGroup1);
        rdoGroup2 = (RadioGroup) findViewById(R.id.RadioGroup2);

        txtVolume = (EditText) findViewById(R.id.txtVolume);
        txtABV = (EditText) findViewById(R.id.txtABV);
        txtCost = (EditText) findViewById(R.id.txtCost);

        btnCalculate = (Button) findViewById(R.id.btnCalculate);
        // disable calculate button
        btnCalculate.setEnabled(false);
        btnClear = (Button) findViewById(R.id.btnClear);


        txtTotalVolume = (TextView) findViewById(R.id.txtTotalVolume);
        txtTotalAlcohol = (TextView) findViewById(R.id.txtTotalAlcohol);
        txtDrinkSize = (TextView) findViewById(R.id.txtDrinkSize);
        txtNumberofDrinks = (TextView) findViewById(R.id.txtNumberofDrinks);
        txtCostperDrink = (TextView) findViewById(R.id.txtCostperDrink);


        /** Key listeners */
        txtContainers.setOnKeyListener(mKeyListener);
        txtVolume.setOnKeyListener(mKeyListener);
        txtABV.setOnKeyListener(mKeyListener);
        txtCost.setOnKeyListener(mKeyListener);

        /** buttons */
        btnCalculate.setOnClickListener(mClickListener);
        btnClear.setOnClickListener(mClickListener);

        // listener events
        rdoGroup2.setOnCheckedChangeListener(new OnCheckedChangeListener() {


            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (checkedId == R.id.radioOZ
                        || checkedId == R.id.radioOZ) {

                }

                if (checkedId == R.id.radiomL
                        || checkedId == R.id.radiomL) {


                }
                // Check selection
                radioCheckedId = checkedId;
            }
        });

        rdoGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {


            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (checkedId == R.id.radioPercent
                        || checkedId == R.id.radioPercent) {

                }

                if (checkedId == R.id.radioProof
                        || checkedId == R.id.radioProof) {


                }
                // Check selection
                radioCheckedId = checkedId;
            }
        });

        /** Key listeners */
        txtContainers.setOnKeyListener(mKeyListener);
        txtVolume.setOnKeyListener(mKeyListener);
        txtABV.setOnKeyListener(mKeyListener);
        txtCost.setOnKeyListener(mKeyListener);

        /** buttons */
        btnCalculate.setOnClickListener(mClickListener);
        btnClear.setOnClickListener(mClickListener);
}

    private OnKeyListener mKeyListener = new OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {

            switch (v.getId()) {
            case R.id.txtContainers:
            case R.id.txtVolume:
            case R.id.txtABV:
                btnCalculate.setEnabled(txtVolume.getText().length() > 0
                            && txtContainers.getText().length() > 0
                            && txtCost.getText().length() > 0
                            && txtABV.getText().length() > 0);
                break;
            case R.id.txtCost:
                btnCalculate.setEnabled(txtVolume.getText().length() > 0
                        && txtABV.getText().length() > 0
                        && txtContainers.getText().length() > 0
                        && txtCost.getText().length() > 0);
                break;
            }
            return false;
        }

    };

        // click listener actions
        private OnClickListener mClickListener = new OnClickListener() {

            public void onClick(View v) {
                if (v.getId() == R.id.btnCalculate) {
                    calculate();
                } else {
                    clear();
                }
            }
        };

        // calculation section
    private void calculate() {
        Double containers = Double.parseDouble(txtContainers.getText().toString()); 
        Double volume = Double.parseDouble(txtVolume.getText().toString());
        Double abv = Double.parseDouble(txtABV.getText().toString());
        Double cost = Double.parseDouble(txtCost.getText().toString());
        Double percentage = null;
        Double volumeunit = null;
        boolean isError = false;



        if (radioCheckedId == -1) {
            radioCheckedId = rdoGroup1.getCheckedRadioButtonId();
        }
        if (radioCheckedId == R.id.radioPercent) {
            percentage = 1.00;
        } else if (radioCheckedId == R.id.radioProof) {
            percentage = 2.00;

        }


        if (radioCheckedId == -1) {
            radioCheckedId = rdoGroup2.getCheckedRadioButtonId();
        }

        if (radioCheckedId == R.id.radioOZ) {
            volumeunit = 1.00;
        } else if (radioCheckedId == R.id.radiomL) {
            volumeunit = 29.57;

        }



        /* specific calculations */
        if (!isError) {
            Double totalVolume = (containers * (volume/volumeunit));
            int precision = 100; //keep 2 digits
            totalVolume = Math.floor(totalVolume * precision +.5)/precision;
            Double totalAlcohol = ((containers * (volume/volumeunit)) * ((abv/percentage) / 100));
            totalAlcohol = Math.floor(totalAlcohol * precision +.5)/precision;
            Double drinkSize =  ((containers * (volume/volumeunit)) / ((containers * (volume/volumeunit)) * ((abv/percentage) / 100) / 0.6));
            drinkSize = Math.floor(drinkSize * precision +.5)/precision;
            Double numberofDrinks =  ((containers * (volume/volumeunit)) * ((abv/percentage)/ 100) / 0.6);
            numberofDrinks = Math.floor(numberofDrinks * precision +.5)/precision;
            Double costperDrink =  (cost / ((containers * (volume/volumeunit)) * ((abv/percentage)/ 100) / 0.6));
            costperDrink = Math.floor(costperDrink * precision +.5)/precision;

            txtTotalVolume.setText(totalVolume.toString());
            txtTotalAlcohol.setText(totalAlcohol.toString());
            txtDrinkSize.setText(drinkSize.toString());
            txtNumberofDrinks.setText(numberofDrinks.toString());
            txtCostperDrink.setText(costperDrink.toString());
        }
    }

    // clear functions
    private void clear() {
        txtTotalVolume.setText("");
        txtTotalAlcohol.setText("");
        txtDrinkSize.setText("");
        txtNumberofDrinks.setText("");
        txtCostperDrink.setText("");
        txtContainers.setText("1");
        txtVolume.setText("0");
        txtABV.setText("0");
        txtCost.setText("0.00");

        // set focus on the first field
        txtVolume.requestFocus();

    }

}

0 个答案:

没有答案