遗憾的是,在一个简单的数学问题上,我被困住了。 我正在创建一个咖啡应用程序,如下所示,它取“数量”并将其乘以价格以创建每个饮料饮料的小计。然后它将从饮料中取出所有小计,并将它们一起添加到底部的输出中,这将自动更新。
我已经处理了从0添加或减去的代码(我仍然无法找到防止它低于0的方法,如果有人有想法,请帮助我们)。 我有它,所以前两个按钮(粉红色)现在适用于Espresso和Macchiato。
这是我的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
}
}
现在,我可能必须将总价格设置为文本视图,而不是编辑文本,想法?
因此,在实施了来自用户@ 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)
答案 0 :(得分:1)
简单检查可以防止负值(使用按钮):
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));
}
});
要防止手动输入负值,您可以将android:digits="0123456789"
设置为@ShobhitPuri建议。
然后,您需要设置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);
...
答案 1 :(得分:0)
“我仍然无法想办法防止它低于0”
您可以通过一种方式将android:digits="0123456789"
添加到xml文件中的EditText
。这将阻止用户输入除这些号码之外的任何内容。
另一种方法是检查onClickListener
。在此,您需要使用EditText
检查所有ed.getText()
的值,然后检查其整数是否在您可接受的范围内。
另一种方法是在EditText
上使用addTextChangedListener。你可以听你在那里输入什么。当小于0或者放入无效字符时,你可以检查条件。(最好限制键盘,以防止垃圾进入首位)。