gpt4 book ai didi

java - 将总计汇总到编辑文本输出并防止负数

转载 作者:行者123 更新时间:2023-12-01 12:16:06 30 4
gpt4 key购买 nike

遗憾的是,我被一个简单的数学问题困住了。我正在创建一个咖啡应用程序,如下所示,它采用“数量”并将其乘以价格来创建每种饮料的小计。然后,它将获取饮料中的所有小计,并将它们加在一起,作为底部的输出,该输出将自动更新。

drink Menu

我已经编写了从 0 进行加或减的代码(我仍然想不出一种方法来防止它低于 0,如果有人有想法,请提供帮助)。我拥有它,因此前 2 个按钮(粉色)现在可以用于浓缩咖啡和玛奇朵。

这是我的 Java 文件

package com.example.cofeeshop;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class DrinkMenu extends Activity {
EditText quantity, quantity2, total;

Button button, plus1, minus1, plus2, minus2;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drinkmenu);
addListenerOnButton();

// area for the espresso
plus1 = (Button) findViewById(R.id.button6);
minus1 = (Button) findViewById(R.id.button7);
quantity = (EditText) findViewById(R.id.editText2);

// area for the macchiato
plus2 = (Button) findViewById(R.id.button8);
minus2 = (Button) findViewById(R.id.button9);
quantity2 = (EditText) findViewById(R.id.editText4);

//subtotal for espresso
//subtotal for macchiato

total = (EditText) findViewById(R.id.editText9);

plus1.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View V){

String numb1 = quantity.getText().toString();

int num1 = Integer.parseInt(numb1);
int inum1 = num1+1;
quantity.setText(Integer.toString(inum1));
}
});//plus1 button
minus1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1-1;
quantity.setText(Integer.toString(inum1));

}
});//minus1 button

plus2.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View V){

String numb2 = quantity2.getText().toString();

int num2 = Integer.parseInt(numb2);
int inum2 = num2+1;
quantity2.setText(Integer.toString(inum2));
}
});//plus2 button
minus2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2-1;
quantity2.setText(Integer.toString(inum2));

}
});//minus2 button
// Here is where I think I should place the sub-total multiplied by the prices
// and will be out put to the total = espresso_sub_total*espress_price +
// macchiato_sub_total*macchiato_price + and so on for the other drinks

}

}

现在,我可能必须将总价设置为 TextView 而不是编辑文本,您认为吗?

因此,在实现了用户 @useruser3249477 和 @Shobhit 的想法后,我得到的数字不再低于 0 和高于 10,但随后我尝试将总数添加到“0”的总区域中。我已经更新了下面的 java 和 xml 文件的代码。当我按下“+”按钮时它崩溃了。

更新的 Java 源代码:

package com.example.cofeeshop;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class DrinkMenu extends Activity {
EditText quantity, quantity2;
//TextView total;
Button button, plus1, minus1, plus2, minus2;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drinkmenu);
addListenerOnButton();

// area for the espresso
plus1 = (Button) findViewById(R.id.button6);
minus1 = (Button) findViewById(R.id.button7);
quantity = (EditText) findViewById(R.id.editText2);

// area for the macchiato
plus2 = (Button) findViewById(R.id.button8);
minus2 = (Button) findViewById(R.id.button9);
quantity2 = (EditText) findViewById(R.id.editText4);

//espresso-sub-total
//macchiato-sub-total

//total = (TextView) findViewById(R.id.textView7);


plus1.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View V){

String numb1 = quantity.getText().toString();

int num1 = Integer.parseInt(numb1);
int inum1 = num1+1;
if (inum1 > 10) return;
quantity.setText(Integer.toString(inum1));
}
});//plus1 button
minus1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1-1;
if (inum1 < 0) return;
quantity.setText(Integer.toString(inum1));

}
});//minus2 button

plus2.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View V){

String numb2 = quantity2.getText().toString();

int num2 = Integer.parseInt(numb2);
int inum2 = num2+1;
if (inum2 > 10) return;
quantity2.setText(Integer.toString(inum2));
}
});//plus1 buttons
minus2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2-1;
if (inum2 < 0) return;
quantity2.setText(Integer.toString(inum2));

}
});

// double subtotal = Double.parseDouble(numb1);
// Here is where I think I should place the sub-total multiplied by the prices
// and will be out put to the total = num1*3;


final TextView total = (TextView) findViewById(R.id.textView7);
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Remove previous price of these items
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal - count*3;
total.setText(newTotal);
}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Add the new items price
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal + count*3;
total.setText(newTotal);
}

@Override
public void afterTextChanged(Editable editable) {}
};

quantity.addTextChangedListener(textWatcher);
quantity2.addTextChangedListener(textWatcher);
}
//order button code that is useless to this question.
}

这是我已更新的 XML 文件:

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/button"
android:layout_marginRight="75dp"
android:layout_toLeftOf="@+id/textView2"
android:layout_toStartOf="@+id/textView2"
android:background="#FFFFFF"
android:text="Drinks:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="24dp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/orderbtn"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/orderbtn"
android:background="#FFFFFF"
android:text="Quantity:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="24dp"
android:textStyle="bold" />

<Button
android:id="@+id/button"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Espresso"
android:textSize="24dp" />

<Button
android:id="@+id/button2"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Macchiato"
android:textSize="24dp" />

<Button
android:id="@+id/button3"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button2"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Con Panna"
android:textSize="24dp" />

<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button4"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Latte"
android:textSize="24dp" />

<Button
android:id="@+id/button6"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/button"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />

<EditText
android:id="@+id/editText2"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_alignTop="@+id/button"
android:layout_toLeftOf="@+id/button6"
android:layout_toStartOf="@+id/button6"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />

<Button
android:id="@+id/button7"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button"
android:layout_toLeftOf="@+id/editText2"
android:layout_toStartOf="@+id/editText2"
android:background="#ffff45df"
android:text="-"
android:textSize="24dp" />

<Button
android:id="@+id/button8"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/button2"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />

<EditText
android:id="@+id/editText3"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_toLeftOf="@+id/button8"
android:layout_toStartOf="@+id/button8"
android:background="#FFFFFF"
android:ems="10"
android:inputType="number"
android:text=" 0"
android:textSize="24dp" />

<EditText
android:id="@+id/editText4"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button8"
android:layout_alignTop="@+id/button2"
android:layout_toLeftOf="@+id/button8"
android:layout_toStartOf="@+id/button8"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24sp" />

<Button
android:id="@+id/button9"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_toLeftOf="@+id/editText3"
android:layout_toStartOf="@+id/editText3"
android:background="#ffff45df"
android:text="-"
android:textSize="24dp" />

<Button
android:id="@+id/button10"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/button8"
android:layout_alignRight="@+id/button8"
android:layout_alignTop="@+id/button3"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />

<Button
android:id="@+id/button11"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/button4"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />

<Button
android:id="@+id/button12"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/button5"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />

<EditText
android:id="@+id/editText5"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_above="@+id/button4"
android:layout_alignTop="@+id/button3"
android:layout_toLeftOf="@+id/button10"
android:layout_toStartOf="@+id/button10"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />

<EditText
android:id="@+id/editText6"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button11"
android:layout_alignTop="@+id/button4"
android:layout_toLeftOf="@+id/button11"
android:layout_toStartOf="@+id/button11"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />

<EditText
android:id="@+id/editText7"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button12"
android:layout_alignTop="@+id/button5"
android:layout_toLeftOf="@+id/button12"
android:layout_toStartOf="@+id/button12"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />

<Button
android:id="@+id/button14"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button4"
android:layout_toLeftOf="@+id/editText6"
android:layout_toStartOf="@+id/editText6"
android:background="#ffff45df"
android:text="-"
android:textSize="24sp" />

<Button
android:id="@+id/button15"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button5"
android:layout_toLeftOf="@+id/editText7"
android:layout_toStartOf="@+id/editText7"
android:background="#ffff45df"
android:text="-"
android:textSize="24sp" />

<Button
android:id="@+id/button16"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button3"
android:layout_toLeftOf="@+id/editText5"
android:layout_toStartOf="@+id/editText5"
android:background="#ffff45df"
android:text="-"
android:textSize="24sp" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button9"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button16"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button14"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />

<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button15"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />

<Button
android:id="@+id/button4"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button3"
android:layout_marginTop="32dp"
android:background="#FFFFFF"
android:text="Americano"
android:textSize="24dp"
android:textColor="#000000" />

<Button
android:id="@+id/orderbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="@drawable/buttonround"
android:text="Order"
android:textSize="24sp" />

<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/orderbtn"
android:layout_alignBottom="@+id/orderbtn"
android:layout_alignParentLeft="true"
android:background="#FFFFFF"
android:text="Total: $"
android:textColor="#000000"
android:textSize="24sp" />

<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView8"
android:layout_alignBottom="@+id/textView8"
android:layout_alignLeft="@+id/textView"
android:layout_alignRight="@+id/textView6"
android:layout_alignStart="@+id/textView"
android:background="#FFFFFF"
android:text="0"
android:textSize="24sp" />

</RelativeLayout>

以下是一些出现的错误。

E/AndroidRuntime(369):致命异常:主

E/AndroidRuntime(369): android.content.res.Resources$NotFoundException: 字符串资源 ID #0x0//似乎在这里

E/AndroidRuntime(369):位于 android.content.res.Resources.getText(Resources.java:201)

最佳答案

简单的检查将防止负值(使用按钮):

minus2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2-1;
if (inum2 < 0) return;
quantity2.setText(Integer.toString(inum2));
}
});

为了防止手动输入负值,您可以按照@ShobhitPuri的建议设置android:digits="0123456789"

然后,对于总数,您需要设置 TextWatchers:

final EditText total = (EditText) findViewById(R.id.editText9);
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Remove previous price of these items
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal - count*3;
total.setText(newTotal);
}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Add the new items price
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal + count*3;
total.setText(newTotal);
}

@Override
public void afterTextChanged(Editable editable) {}
};

// Now set the TextWatcher on every count EditText
// If you have different prices, you'll need multiple TextWatchers
quantity1.addTextChangedListener(textWatcher);
quantity2.addTextChangedListener(textWatcher);

...

关于java - 将总计汇总到编辑文本输出并防止负数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27006045/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com