Я пытаюсь создать приложение для Android, которое поможет рассчитать разделение счета, если, скажем, вы едите в компании из 2 человек или более.
Вы должны ввести промежуточный итог счета, ввести количество человек в группе, ввести применимую скидку, если есть, есть 2 флажка для налога в размере 7% и сбор за обслуживание в размере 10%, еслиеще не включены в счет.Наконец, вам просто нужно нажать на кнопку «Рассчитать» для приложения, чтобы вычислить, сколько каждый человек должен заплатить.
Мои вопросы:
- для промежуточного итога, он долженбыть двойным вместо int, но я не уверен, как разобрать String в double.Есть ли способ сделать это?
- Я не уверен, что это лучший способ активировать флажки для налога и 10% советов
- Когда я нажимаю кнопку расчета,Предполагается, что будет отображаться сообщение Toast с результатом расчета, но ничего не появится.Я не уверен, что проблема в parseInteger, checkBoxes, или в том, что метод onClick неправильный, или во всех них.
Вот код, который я написал:
package com.kevinw.BillSplitter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class BillSplitter extends Activity implements OnClickListener {
/** Declares XML Widgets */
private EditText numberDiners;
private EditText enterAmount;
private EditText enterDiscount;
private CheckBox gst;
private CheckBox tips;
private CheckBox cess;
double result;
private Button calculate;
private TextView resultAmount;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Initialize Widgets
numberDiners = (EditText) findViewById(R.id.numberDiners);
enterAmount = (EditText) findViewById(R.id.EnterAmount);
enterDiscount = (EditText) findViewById(R.id.EnterDiscount);
calculate = (Button) findViewById(R.id.calculate);
//Initialize CheckBoxes
gst = (CheckBox) findViewById(R.id.cbCheck1);
gst.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (gst.isChecked()) {
result = result + (0.07 * result);
}
else {
result = result;
}
}
});
tips = (CheckBox) findViewById(R.id.cbCheck2);
tips.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (tips.isChecked()) {
result = result + (0.1 * result);
}
else {
result = result;
}
}
});
}
@Override
public void onClick(View v) {
//Initialize EditTexts
String amount = enterAmount.getText().toString();
int subtotal = Integer.parseInt(amount);
String diners = numberDiners.getText().toString();
int people = Integer.parseInt(diners);
String disc = enterDiscount.getText().toString();
int discount = Integer.parseInt(disc);
double discounted = discount / 100;
result = (1 - discounted) * (subtotal / people);
switch (v.getId()) {
case(R.id.calculate):
Toast.makeText(this, "The Amount a Person has to pay: $" + result, Toast.LENGTH_LONG).show();
break;
}
}
}
и, если это поможет, это код XML для макета:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/dinersView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"/>
<TextView
android:id="@+id/Enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/dinersView"
android:layout_alignLeft="@+id/EnterAmount"
android:text="@string/enter"/>
<EditText
android:id="@+id/numberDiners"
android:layout_height="wrap_content"
android:layout_below="@+id/dinersView"
android:layout_width="100dip"/>
<EditText
android:id="@+id/EnterAmount"
android:layout_height="wrap_content"
android:layout_below="@+id/Enter"
android:layout_toRightOf="@+id/numberDiners"
android:layout_width="220dip"/>
<TextView
android:id="@+id/Discount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/EnterAmount"
android:layout_alignLeft="@+id/EnterAmount"
android:text="@string/discount"/>
<EditText
android:id="@+id/EnterDiscount"
android:layout_height="wrap_content"
android:layout_below="@+id/Discount"
android:layout_alignLeft="@+id/Discount"
android:layout_width="220dip"/>
<CheckBox
android:id="@+id/cbCheck1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/EnterDiscount" />
<CheckBox
android:id="@+id/cbCheck2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cbCheck1" />
<TextView
android:id="@+id/gst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/cbCheck1"
android:layout_alignTop="@+id/cbCheck1"
android:layout_alignLeft="@+id/enterDiscount"
android:layout_marginTop="10dip"
android:text="@string/GST"/>
<TextView
android:id="@+id/tips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/cbCheck2"
android:layout_alignTop="@+id/cbCheck2"
android:layout_marginTop="10dip"
android:text="@string/tips"/>
<Button
android:id="@+id/calculate"
android:layout_below="@+id/cbCheck2"
android:layout_width="wrap_content"
android:text="@string/calculate"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
</RelativeLayout>
Спасибо за помощь.Очень ценю это.