如何设置发起toast对象的订单数量的最大值和最小值?

How to set a max and min value for an order quantity that initiates a toast object?

我的应用程序接收客户姓名、订购的咖啡数量以及客户是否想要生奶油 and/or 巧克力。它根据客户订单提供总价的实时更新,一旦下订单,它将为客户提供订单摘要。顾客不能订购负数或超过 10 杯咖啡。我已经创建了数量计数器,但需要帮助执行 TOAST 对象。 这是我的 MainActivity java 文件:

public class MainActivity extends AppCompatActivity {

    ArrayList<String> selections = new ArrayList<String>();
    TextView order_summary;
    private Button _decrease;
    private Button _increase;
    private TextView _quantity;
    private static int _counter = 1;
    private String _stringVal;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        order_summary = (TextView)findViewById(R.id.textOrder);
        order_summary.setEnabled(false);

        _decrease = (Button) findViewById(R.id.btnSubtract);
        _increase = (Button) findViewById(R.id.btnAdd);
        _quantity = (TextView) findViewById(R.id.textQty);

        _decrease.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.d("src", "Decreasing value...");
                _counter--;
                _stringVal = Integer.toString(_counter);
                _quantity.setText(_stringVal);
            }
        });

        _increase.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.d("src", "Decreasing value...");
                _counter--;
                _stringVal = Integer.toString(_counter);
                _quantity.setText(_stringVal);
            }
        });

        _increase.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.d("src", "Increasing value...");
                _counter++;
                _stringVal = Integer.toString(_counter);
                _quantity.setText(_stringVal);
            }
        });

    };

    // start Order class
    public class Order {
        // declare order variables
        private int quantity;
        private double price;
        private double total = quantity * price;
    }

    // declare price variables
    private double coffeePrice = 5.00;
    private double whippedPrice = 1.00;
    private double chocolatePrice = 1.00;

    // adds selected items to order summary
    public void selectItem (View v) {
        boolean checked = ((CheckBox) v).isChecked();
            switch (v.getId())
            {
                case R.id.chkWhipped:
                    if(checked)
                    {selections.add("Whipped Cream");}
                else
                    {
                        selections.remove("whipped Cream");
                    }
                break;
                case R.id.chkChocolate:
                if(checked)
                {selections.add("Chocolate");}
                else
                {
                    selections.remove("Chocolate");
                }
                break;
            }
    }

    // display order summary
    public void finalSelection (View v) {
        String final_order = "";
            for(String Selections : selections){
                final_order = order_summary + Selections + "\n";
            }
        order_summary.setText(final_order);
            order_summary.setEnabled(true);
    }

    //display in long period of time
    //Toast.makeText(getApplicationContext(), "You cannot order a negative number or more than 10 coffees",
    //Toast.LENGTH_LONG).show();

我在底部有一个 TOAST 脚本被注释掉了,不确定能否正确实现它。

您可以对按下的按钮执行检查:

    _decrease.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(_counter <= 0) {
                 Log.d("src", "Decreasing value...");
                 _counter--;
                 _stringVal = Integer.toString(_counter);
                 _quantity.setText(_stringVal);

                 Toast.makeText(getApplicationContext(), "You cannot order a negative number or more than 10 coffees",
                 Toast.LENGTH_LONG).show();
            }
        }
    });

可以在 _increase 按钮​​上做同样的事情。