试图从两个不同的按钮点击中添加两个数字的总和

时间:2017-10-27 02:50:03

标签: java android

imageview burger = 7.99当点击时,imageview fries = 2.99点击时,我想得到两者的总和但是当我一个接一个地点击它刷新并且只是在里面添加值而不是添加到前一个值。我觉得解决方案就在我面前,但我仍然不知道我在这里缺少什么............

public class ThirdActivity extends AppCompatActivity {

static double price1 = 0;
static double price2 = 0;
TextView total;
double bill;
static int but1_value = 0;
static int but1_value1 = 0;
ImageView burger;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_third);

    TextView tab = (TextView) findViewById(R.id.Table_Size);

    burger = (ImageView) findViewById(R.id.burger);

    total = (TextView) findViewById(R.id.price1);

    final ImageView fries = (ImageView) findViewById(R.id.fries);

    fries.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {



            price2 = price2 + 2.99;

            total.setText("Total: " + price2);


        }
    });


    burger.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            price1 = price1 + 7.99;

            total.setText("Total: " + price1);

        }
    });

3 个答案:

答案 0 :(得分:0)

Dim ie As Object Private Sub CommandButton1_Click() On error go to ErrMsg Set ie = CreateObject("InternetExplorer.Application") ie.Visible = True ie.Navigate TextBox1.Text Do DoEvents Loop Until ie.readystate = 4 Application.Wait (Now + TimeValue("00:00:02")) ErrMsg: End Sub Private Sub CommandButton2_Click() Dim i As Long Dim prfl As String Dim ws As Worksheet Set ws = ActiveSheet Dim INPTTG, btn As Object Dim Profile_link As String Dim H3tg As Object Dim iedata As Object On error go to msgErr If iedata Is Nothing Then Set iedata = CreateObject("InternetExplorer.Application") End If iedata.Visible = True For i = TextBox2.Text To TextBox3.Text prfl = ws.Range("E" & i).Value For Each INPTTG In ie.document.getelementsbytagname("input") If INPTTG.classname = "gs_in_txt" Then INPTTG.Value = "" INPTTG.Value = prfl Exit For End If Next INPTTG 'button Application.Wait (Now + TimeValue("00:00:02")) For Each btn In ie.document.getelementsbytagname("button") If btn.ID = "gs_hdr_tsb" Then btn.Click Do DoEvents Loop Until ie.readystate = 4 Application.Wait (Now + TimeValue("00:00:02")) End If Next btn 'prfl For Each H3tg In ie.document.getelementsbytagname("h3") If H3tg.classname = "gsc_oai_name" And VBA.Trim(H3tg.innertext) = prfl Then Call GetData(H3tg.getelementsbytagname("a").Item(0).href, iedata, i) Exit For End If Next H3tg TextBox2.Text = i Next i MsgBox "Completed" iedata.Quit Set iedata = Nothing Application.DisplayAlerts = False ThisWorkbook.Save MsgErr: End Sub total中,您要设置price1price2

你应该设置两个加法,作为你的目标。

total.setText("Total: " + (price1 + price2));

答案 1 :(得分:0)

您已从一个地方为变量price1分配了一个新值,并从另一个地方为price2分配了一个新值。

如果要维护该值,请使用单个变量并从两个位置保存,或者直接添加它们并设置值。

使用:

total.setText("Total: " + (price1+price2));

我可能没有理解你的问题。随意澄清一下。

答案 2 :(得分:0)

最佳解决方案是创建全局变量total_price。

private double total_price = 0;

每当您点击薯条时更新total_price的值

total_price += price1

和汉堡

total_price += price2

然后将总计设为

total.setText("Total: " + total_price);