我正在尝试开发一款Android移动应用,需要对发票进行一些计算。当我需要将项目添加到发票时,我需要通过单击按钮转到另一个表单(这是另一个活动)。在填写该表格后,只有该特定项目的价值才会到达发票。同样,如果我需要购买X个项目,我需要将该项目填写X次。并且每个项目的金额应单独显示在发票中。最后,通过单击发票中的按钮,可以找到发票中的值的总金额。
这是我实际尝试的内容,但我只能将一个值传递给项目活动的发票表单。当我再次单击添加按钮并填写项目表格并进入发票时,之前输入的值将消失。我需要在发票表单中保留所有值,最后需要计算发票总额。 请帮我解决这个问题。我已经阅读了很多关于此的文章和问题,但我找不到更好的方法来解决这个问题。我非常感谢您的帮助..
第一个XML文件是activity_invoice.XML文件 第二个发票活动,第三个是activity_items.xml,第四个是项目活动
<!--Click this button to goto Items Activity-->
<Button
android:id="@+id/addItemBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add a New Item"
android:background="@drawable/buttonshapes"
/>
<TextView
android:id="@+id/valueofItemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sub Value"
/>
<!--Get the final value of Item activity here-->
<EditText
android:id="@+id/valueofItemEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Sub Value"
/>
<!--Button to find the discount of invoice-->
<Button
android:id="@+id/invoiceDiscountBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total"
/>
<!--Enter the discount value of Invoivce activity-->
<EditText
android:id="@+id/editDiscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal"
/>
<!--get the total sum of all items values in this Invoice activity taken from items Activity-->
<Button
android:id="@+id/invoiceTotaltBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total"
/>
<!--Display the total of invoice-->
<EditText
android:id="@+id/invoiceTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal"
/>
public class Invoice extends AppCompatActivity {
Button addItemBtn, discountBtn, totalBtn;
TextView billedtoUser;
EditText eSubTotal, eDiscount, eTotal;
double invoiceDiscount, subTotalValue, invoiceTotal, subtractDis;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invoice);
setTitle("Invoice");
/*----------------------Add item description--------------------*/
addItemBtn = (Button) findViewById(R.id.addItemBtn);
addItemBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addItemDescription();
}
});
/*-----------------get Items Activity Value to Invoice Activity -------------------------*/
eSubTotal = (EditText) findViewById(R.id.valueofItemText);
Intent myIntent = getIntent();
if (myIntent.hasExtra("ItemValue")) {
valueofItemText.setText(" $" + myIntent.getStringExtra("ItemValue"));
}
/*--------------------define addItemDescription------------------------*/
private void addItemDescription() {
Intent intent = new Intent(Invoice.this, Items.class);
startActivity(intent);
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2">
<ScrollView
android:layout_width="576dp"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="0.03"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:id="@+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:layout_weight="0.6"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Item Code"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FF042A51" />
<EditText
android:id="@+id/itemCode"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_weight="1.12"
android:imeOptions="actionNext"
android:maxLines="1"
android:paddingLeft="10dp"
android:textColor="#000000"
android:theme="@style/MyEditTextTheme" />
</LinearLayout>
<LinearLayout
android:id="@+id/secondLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:layout_weight="0.6"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Item Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FF042A51" />
<EditText
android:id="@+id/itemName"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_weight="1.12"
android:imeOptions="actionNext"
android:maxLines="1"
android:paddingLeft="10dp"
android:textColor="#FF0D68C4"
android:theme="@style/MyEditTextTheme" />
</LinearLayout>
<LinearLayout
android:id="@+id/fourthLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/textView5"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:layout_weight="0.6"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Quantity"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FF042A51" />
<!--Enter the number of Items purchasing -->
<EditText
android:id="@+id/quantity"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_weight="1.11"
android:imeOptions="actionNext"
android:inputType="numberDecimal"
android:maxLines="1"
android:paddingLeft="10dp"
android:textColor="#FF0D68C4"
android:theme="@style/MyEditTextTheme" />
</LinearLayout>
<LinearLayout
android:id="@+id/sixthLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:layout_weight="0.6"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Unit Cost"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FF042A51" />
<!--Enter value of one item-->
<EditText
android:id="@+id/unitCost"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_weight="1.12"
android:imeOptions="actionNext"
android:inputType="numberDecimal"
android:maxLines="1"
android:paddingLeft="10dp"
android:textColor="#000000"
android:theme="@style/MyEditTextTheme" />
</LinearLayout>
<LinearLayout
android:id="@+id/seventhLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:layout_weight="0.6"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Discount (%)"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FF042A51" />
<!--Enter discount for items If discount is there-->
<EditText
android:id="@+id/discount"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_weight="1.12"
android:imeOptions="actionNext"
android:inputType="numberDecimal"
android:maxLines="1"
android:paddingLeft="10dp"
android:textColor="#000000"
android:theme="@style/MyEditTextTheme" />
</LinearLayout>
<LinearLayout
android:id="@+id/eigthLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="2">
<!--Button to find the amount for items-->
<Button
android:id="@+id/amountbtn"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:layout_weight="0.6"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Line Value"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FF042A51" />
<!--Display the amount of Items-->
<EditText
android:id="@+id/amount"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_weight="1.12"
android:imeOptions="actionNext"
android:inputType="numberDecimal"
android:maxLines="1"
android:paddingLeft="10dp"
android:textColor="#000000"
android:theme="@style/MyEditTextTheme" />
</LinearLayout>
<!--Submit this value to Invoice Activity using this button-->
<Button
android:id="@+id/submitDescription"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="15dp"
android:background="@drawable/roundbtn"
android:gravity="center"
android:text="Done"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
android:textStyle="bold"
android:minWidth="20dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
public class Items extends AppCompatActivity {
Button descriptionSubmitBtn, finalAmountBtn;
EditText iCode, iName;
EditText qty, uCost, discount, famountText;
double unitCost, discountValue, quantity;
double quntUniCost, subtractDis, finalTotalAmount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_items_description);
setTitle("Item Description");
descriptionSubmitBtn = (Button) findViewById(R.id.submitDescription);
iCode = (EditText) findViewById(R.id.itemCode);
iName = (EditText) findViewById(R.id.itemName);
/*--------------------variables to find discount for each item---------------------------*/
qty = (EditText) findViewById(R.id.quantity);
uCost = (EditText) findViewById(R.id.unitCost);
discount = (EditText) findViewById(R.id.discount);
famountText = (EditText) findViewById(R.id.amount);
finalAmountBtn = (Button) findViewById(R.id.amountbtn);
/*------------------calculateDiscount-------------------------------------*/
finalAmountBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculateDiscount();
}
});
/*--------------------------sendData----------------------------------------*/
descriptionSubmitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendData();
}
});
}
/*------------------------------define sendData method-----------------------------*/
private void sendData() {
String str1 = famountText.getText().toString();
Intent intent = new Intent(Items.this,Invoice.class);
intent.putExtra("ItemValue", str1); //key Value pair
startActivity(intent);
}
/*--------------------define calculateDiscount method-------------------------------*/
private void calculateDiscount() {
quantity = Double.parseDouble((qty.getText().toString()));
unitCost = Double.parseDouble(uCost.getText().toString());
discountValue = Double.parseDouble(discount.getText().toString());
quntUniCost = quantity * unitCost;
subtractDis = 100 - discountValue;
finalTotalAmount = (subtractDis * quntUniCost) / 100;
//round to two decimal points
famountText.setText(String.format("%.2f", finalTotalAmount));
}
}
答案 0 :(得分:2)
您可以使用listView
并将项目添加到该列表中,并将其显示在发票活动中。目前,您正在传递intent.putextra ();
中的一个值,当您再次调用发票活动时会刷新该值,因为正在调用onCreate()
并且您再次创建了查看。
另一个选项是使用SharedPreferences
来存储您的商品,然后将其显示在发票活动中。
答案 1 :(得分:0)
我在你的代码中看到的一个问题是你使用输入整数或十进制。所以即使传递它你必须考虑类型。此外,在进行计算之后,在将值设置为TextView之前必须转换为String。你不能使用字符串进行任何数学计算,如加,减等。
获取Int Intent //// getIntExtra(“key”,0); 获取String /// getStringEtra(“key”); 得到双重///做研究
在Invoice Class中你已经明确了意图而没有将任何值传递给intent ...基本上,每次你开始新的intent而没有结束你的会话有值时,你必须总是在两个intent之间传递值,
首先,您将值传递给下一个活动,然后从intent获取值并执行您想要执行的任何计算,并再次使用intent传递给第一个活动,再次从第一个活动中您必须从intent获取值您从第二个活动传递并再次将该值和新值发送到第二个活动,我知道它有点值得尝试,但是值得一试,
////ActivityOne.java
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
i.putExtra("Value1", 1);
// callback via this code
startActivity(i);
}
////ActivityTwo.java
eSubTotal = (EditText) findViewById(R.id.valueofItemText);
Intent myIntent = getIntent();
if (myIntent.hasExtra("Value1")) {
valueofItemText.setText(" $" + String.ValueOf(myIntent.getIntExtra("ItemValue", 0)));
}
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), ActivityOne.class);
i.putExtra("Answer1", "Android By Irufan");
startActivity(i);
}
////ActivityOne.java
String answer1 ;
Intent i = getIntent();
answerIntent = i.getStringExtra("Answer1");
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
i.putExtra("Value2", 1);
i.putExtra("Answer1", answerIntent);
startActivity(i);
}