Android似乎没有正确地进行数学运算

时间:2014-10-03 23:09:36

标签: java android math

基本上,有一个文本字段,用户可以在其中输入用餐的小计。然后,当它们点击完成时,do方法应该采用tip字段值,并将其乘以小计值,以便总数包括两个数据。但是,当我点击完成按钮时,结果实际上总是等于小计,并且始终忽略提示。这是代码:

public class MainActivity extends Activity implements OnRatingBarChangeListener {

    // Testing Stuff to show the rating value, will need to use later for maths
    static RatingBar rb;
    TextView tipsTV;

    ImageView greyPlus, greyMinus, greyPlus2, greyMinus2;

    TextView peopleDiningTV, peopleDiningTitle;
    int peopleDining = 2;

    TextView tipValue;
    int tipValueInt = 10;

    TextView subtotal, total;
    TextView subtotalTitle, totalTitle;

    TextView epp, eppTitle;
    Button done;

    // Elements for hiding and such
    static RelativeLayout rl;
    static Button settingsButton;

    public static int rating = 3;

    // The Image used as the DropDown button, Rotate code below
    ImageView dropDownButton;

    Boolean hasRotated = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        dropDownButton = (ImageView) findViewById(R.id.dropDownButton);
        rb = (RatingBar) findViewById(R.id.ratingBar1);
        rb.setRating(rating);

        tipsTV = (TextView) findViewById(R.id.textView2);

        tipValue = (TextView) findViewById(R.id.tipText);

        greyPlus = (ImageView) findViewById(R.id.greyPlus);
        greyMinus = (ImageView) findViewById(R.id.greyMinus);

        greyPlus2 = (ImageView) findViewById(R.id.greyPlus2);
        greyMinus2 = (ImageView) findViewById(R.id.greyMinus2);

        peopleDiningTV = (TextView) findViewById(R.id.textViewPeople);
        peopleDiningTitle = (TextView) findViewById(R.id.TextView02);

        subtotal =(TextView) findViewById(R.id.subtotalText);
        subtotalTitle =(TextView) findViewById(R.id.subtotalTitle);

        total =(TextView) findViewById(R.id.totalText);
        totalTitle =(TextView) findViewById(R.id.totalTitle);

        epp = (TextView) findViewById(R.id.eppText);
        eppTitle = (TextView) findViewById(R.id.eppTitle);

        done = (Button) findViewById(R.id.buttonDone);

        greyPlus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                tipValueInt++;
                tipValue.setText(tipValueInt + "%");
            }
        });
        greyMinus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tipValueInt >= 1) {
                    tipValueInt--;
                    tipValue.setText(tipValueInt + "%");
                }
                if(tipValueInt == 0){
                    tipValue.setText("No Tip.");
                }
            }
        });

        greyPlus2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                peopleDining++;
                peopleDiningTV.setText(peopleDining + "");
            }
        });
        greyMinus2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (peopleDining > 1) {
                    peopleDining--;
                    peopleDiningTV.setText(peopleDining + "");
                }
            }
        }); 
    }

    @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;
    }

    FragmentManager fm = getFragmentManager();
    QuizFragment qf = new QuizFragment();

    public void dropDown(View view) {
        if (hasRotated == false) {
            FragmentTransaction ft = fm.beginTransaction();
            ft.setCustomAnimations(android.R.animator.fade_in,
                    android.R.animator.fade_out);
            dropDownButton.setRotation(90);
            ft.add(R.id.quizFragment, qf);
            ft.show(qf);
            ft.commit();
            hasRotated = true;

            // Hiding Elements, so they don't show through the fragment
            tipsTV.setVisibility(View.INVISIBLE);
            greyPlus.setVisibility(View.INVISIBLE);
            greyMinus.setVisibility(View.INVISIBLE);
            tipValue.setVisibility(View.INVISIBLE);

            greyPlus2.setVisibility(View.INVISIBLE);
            greyMinus2.setVisibility(View.INVISIBLE);
            peopleDiningTV.setVisibility(View.INVISIBLE);
            peopleDiningTitle.setVisibility(View.INVISIBLE);

            subtotal.setVisibility(View.INVISIBLE);
            subtotalTitle.setVisibility(View.INVISIBLE);
            total.setVisibility(View.INVISIBLE);
            totalTitle.setVisibility(View.INVISIBLE);

            epp.setVisibility(View.INVISIBLE);
            eppTitle.setVisibility(View.INVISIBLE);

            done.setVisibility(View.INVISIBLE);
        } else if (hasRotated == true) {
            FragmentTransaction ft = fm.beginTransaction();
            ft.setCustomAnimations(android.R.animator.fade_out,
                    android.R.animator.fade_out);
            dropDownButton.setRotation(0);
            hasRotated = false;
            ft.remove(qf);
            ft.commit();

            // Hiding Elements, so they don't show through the fragment
            tipsTV.setVisibility(View.VISIBLE);
            greyPlus.setVisibility(View.VISIBLE);
            greyMinus.setVisibility(View.VISIBLE);
            tipValue.setVisibility(View.VISIBLE);

            greyPlus2.setVisibility(View.VISIBLE);
            greyMinus2.setVisibility(View.VISIBLE);
            peopleDiningTV.setVisibility(View.VISIBLE);
            peopleDiningTitle.setVisibility(View.VISIBLE);

            subtotal.setVisibility(View.VISIBLE);
            subtotalTitle.setVisibility(View.VISIBLE);
            total.setVisibility(View.VISIBLE);
            totalTitle.setVisibility(View.VISIBLE);

            epp.setVisibility(View.VISIBLE);
            eppTitle.setVisibility(View.VISIBLE);

            done.setVisibility(View.VISIBLE);
        }
    }

    public void openSettings(View view) {
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
    }

    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating,
            boolean fromTouch) {

    }
    public void done(View view){
        int subtotalCost = Integer.parseInt(subtotal.getText().toString());
        int tip = tipValueInt / 100;
        int totalCost = (subtotalCost * tip) + subtotalCost;
        total.setText(totalCost+"");
    }
}

2 个答案:

答案 0 :(得分:2)

您永远不会在按钮变量OnClickListener上放置done,因此您的点击次数会被忽略,这就是价值不会改变的原因。您应该添加侦听器,然后在done()中调用onClick()方法。您还应删除View view done()参数,因为您根本不使用它。

// ...
done = (Button) findViewById(R.id.buttonDone);
// add listener
done.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            done();
        }
    });
// ...

您可能应该将方法done()的名称更改为displayResult()或更有意义且与名为done的按钮变量不相似的名称。实际上,您应该将大多数变量的名称更改为更清楚地描述变量用途的名称。例如,代表按钮的变量done可以是btnDone ...

答案 1 :(得分:1)

您已将 totalCost 声明为 int 。因此,如果 (subtotalCost * tip) 小于1,则 totalCost = subtotalCost 。如果适用,请尝试使用 floats 而不是 ints