如何在android studio中计算百分比总和



我有一个应用程序,我必须用折扣率计算2个和。例如:200+50-10%结果应自动显示在另一个文本上查看而无需单击任何提交按钮我已经通过textWatcher方法完成了添加部分。但我不能做百分比部分这是我的密码。

我的Xml代码:

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="110dp"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/productPriceEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Product Price"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="110dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/deliveryChargesEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Delivery Charges"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="110dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/discountEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Discount"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textview.MaterialTextView
android:id="@+id/prbtTv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginTop="3dp"
android:background="@drawable/textview_border"
android:gravity="center_vertical"
android:padding="15dp"
android:text="Price Before Taxes"
android:textSize="16sp">
</com.google.android.material.textview.MaterialTextView>

我的Java代码:

TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (!productPriceEt.getText().toString().equals("") && 
!deliveryChargeEt.getText().toString().equals("") && 
!discountEt.getText().toString().equals("")){
int temp1 = Integer.parseInt(productPriceEt.getText().toString());
int temp2 = Integer.parseInt(deliveryChargeEt.getText().toString());
int temp3 = Integer.parseInt(discountEt.getText().toString());
prbTv.setText(String.valueOf(temp1 + temp2 - temp3));
}
}
@Override
public void afterTextChanged(Editable editable) {
}
};
productPriceEt.addTextChangedListener(textWatcher);
deliveryChargeEt.addTextChangedListener(textWatcher);
discountEt.addTextChangedListener(textWatcher);

只需将求和的结果首先保存在一个变量中,如下所示:

private val sum = temp1 + temp2

然后像这样计算关闭:

private val off = (temp3/100) * sum

最后在文本视图上设置文本,如下所示:

prbTv.setText(String.valueOf(sum  - off ));

试试这个

Xml文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/productPriceEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Product Price"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/deliveryChargesEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Delivery Charges"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/discountEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Discount"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textview.MaterialTextView
android:id="@+id/prbtTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="3dp"
android:gravity="center_vertical"
android:padding="15dp"
android:text="Price Before Taxes"
android:textSize="16sp" />
</androidx.appcompat.widget.LinearLayoutCompat>

代码

public class MainActivity extends AppCompatActivity {
TextInputEditText productPriceEt, deliveryChargesEt, discountEt;
MaterialTextView prbtTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
productPriceEt = findViewById(R.id.productPriceEt);
deliveryChargesEt = findViewById(R.id.deliveryChargesEt);
discountEt = findViewById(R.id.discountEt);
prbtTv = findViewById(R.id.prbtTv);
productPriceEt.addTextChangedListener(textWatcher);
deliveryChargesEt.addTextChangedListener(textWatcher);
discountEt.addTextChangedListener(textWatcher);

}
private final TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!productPriceEt.getText().toString().equals("") &&
!deliveryChargesEt.getText().toString().equals("") &&
!discountEt.getText().toString().equals("")) {
int amounts = Integer.parseInt(productPriceEt.getText().toString().trim()) +
Integer.parseInt(deliveryChargesEt.getText().toString().trim());
double amount = Double.parseDouble(String.valueOf(amounts));
double res = (amount / 100.0f) * Double.parseDouble(discountEt.getText().toString());
res = amount - res;
prbtTv.setText("Price Before Taxes- " + amount + "nPrice After Discount - " + res);
Toast.makeText(getApplicationContext(), "" + res, Toast.LENGTH_SHORT).show();
} else {
prbtTv.setText("Price Before Taxes- " + 0.00 + "nPrice After Discount - " + 0.00);
}
}
@Override
public void afterTextChanged(Editable s) {
}
};

}

最新更新