复选框检查添加价格

时间:2018-11-09 17:00:53

标签: android android-studio checkbox

我正在尝试制作一个允许用户选择项目的android应用程序,并且该应用程序能够计算总费用。

我是否知道在选中复选框时如何将金额添加到总价值中,以及在未选中复选框时如何减少金额? Tq

namespace ObjectListView_TreeListView
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.xenSnapshotsTreeListView1 = new ObjectListView_TreeListView.FooTreeListView();
            this.olvColumnAction = ((BrightIdeasSoftware.OLVColumn)(new BrightIdeasSoftware.OLVColumn()));
            this.olvColumnNumbSsToKeep = ((BrightIdeasSoftware.OLVColumn)(new BrightIdeasSoftware.OLVColumn()));
            ((System.ComponentModel.ISupportInitialize)(this.xenSnapshotsTreeListView1)).BeginInit();
            this.SuspendLayout();
            // 
            // xenSnapshotsTreeListView1
            // 
            this.xenSnapshotsTreeListView1.AllColumns.Add(this.olvColumnAction);
            this.xenSnapshotsTreeListView1.AllColumns.Add(this.olvColumnNumbSsToKeep);
            this.xenSnapshotsTreeListView1.CellEditUseWholeCell = false;
            this.xenSnapshotsTreeListView1.CheckBoxes = true;
            this.xenSnapshotsTreeListView1.CheckedAspectName = "IsChecked";
            this.xenSnapshotsTreeListView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.olvColumnAction,
            this.olvColumnNumbSsToKeep});
            this.xenSnapshotsTreeListView1.Cursor = System.Windows.Forms.Cursors.Default;
            this.xenSnapshotsTreeListView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.xenSnapshotsTreeListView1.GridLines = true;
            this.xenSnapshotsTreeListView1.Location = new System.Drawing.Point(0, 0);
            this.xenSnapshotsTreeListView1.MultiSelect = false;
            this.xenSnapshotsTreeListView1.Name = "xenSnapshotsTreeListView1";
            this.xenSnapshotsTreeListView1.ShowGroups = false;
            this.xenSnapshotsTreeListView1.ShowImagesOnSubItems = true;
            this.xenSnapshotsTreeListView1.Size = new System.Drawing.Size(800, 450);
            this.xenSnapshotsTreeListView1.TabIndex = 0;
            this.xenSnapshotsTreeListView1.UseAlternatingBackColors = true;
            this.xenSnapshotsTreeListView1.UseCompatibleStateImageBehavior = false;
            this.xenSnapshotsTreeListView1.View = System.Windows.Forms.View.Details;
            this.xenSnapshotsTreeListView1.VirtualMode = true;
            // 
            // olvColumnAction
            // 
            this.olvColumnAction.AspectName = "Action";
            this.olvColumnAction.Text = "Action";
            this.olvColumnAction.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.olvColumnAction.Width = 200;
            // 
            // olvColumnNumbSsToKeep
            // 
            this.olvColumnNumbSsToKeep.AspectName = "NumberToKeep";
            this.olvColumnNumbSsToKeep.Text = "# To Keep";
            this.olvColumnNumbSsToKeep.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            this.olvColumnNumbSsToKeep.Width = 65;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.xenSnapshotsTreeListView1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.xenSnapshotsTreeListView1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private FooTreeListView xenSnapshotsTreeListView1;
        private BrightIdeasSoftware.OLVColumn olvColumnAction;
        private BrightIdeasSoftware.OLVColumn olvColumnNumbSsToKeep;
    }
}

2 个答案:

答案 0 :(得分:0)

也许您想要这样的东西:

//global var
int total = 0;
(...)
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
    Object tag = buttonView.getTag();
    //This tag can be the numeric value you want, can be setted on xml or programatically
    if ( isChecked )
    {
         total += (int)tag;
    }
    else
    {
         total -= (int)tag;
    }

}

答案 1 :(得分:0)

您可以将每个项目的价格保存在每个复选框标签内,然后从那里进行检索。
另外,如果未选中此复选框,则需要减去价格,并将其从文本视图中删除。
像这样的东西,尽管我无法测试这段代码:

ChkCheese.setTag("2.5");
ChkMushroom.setTag("1.0");
ChkBeef.setTag("4.0");
ChkPineapple.setTag("1.0");
ChkOlive.setTag("2.0");
ChkPepperoni.setTag("3.5");
ChkChicken.setTag("2.0");

checkBoxListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Topping = (TextView)findViewById(R.id.textView_TopAns);
        Topping.setText("");

        CheckBox cb = (CheckBox) v;

        double itemPrice = Double.parseDouble(cb.getTag().toString());

        String newText = Topping.getText().toString();
        if (!cb.isChecked()) {
            itemPrice *= -1.0;
            newText = newText.replace(cb.getText().toString(), "").replace(",,", ",");
            if (newText.indexOf(",") == 0 && newText.length() > 1) newText = newText.substring(1);
            Topping.setText(Topping.getText().toString() + "," + cb.getText().toString());
        } else {
            Topping.setText(newText + "," + cb.getText().toString());
        }

        ToppingPrice += itemPrice;
        TotalPrice = SizePrice + CrustPrice + ToppingPrice;
        Price.setText("RM" + TotalPrice );
    }
} ;
ChkCheese.setOnClickListener(checkBoxListener);
ChkMushroom.setOnClickListener(checkBoxListener);
ChkBeef.setOnClickListener(checkBoxListener);
ChkPineapple.setOnClickListener(checkBoxListener);
ChkOlive.setOnClickListener(checkBoxListener);
ChkPepperoni.setOnClickListener(checkBoxListener);
ChkChicken.setOnClickListener(checkBoxListener);