java - 我将如何实现使用 Android 中的按钮按下来增加 double 值的逻辑

标签 java android button if-statement onclick

我已经创建了按钮

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.paulk.tipcalc.TipCalc" >

        <EditText
            android:id="@+id/BillEditText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/billTextView"
            android:layout_alignBottom="@+id/billTextView"
            android:layout_toEndOf="@+id/billTextView"
            android:layout_toRightOf="@+id/billTextView"
            android:ems="5"
            android:inputType="numberDecimal"
            android:text="@string/bill_edit_text" >

            <requestFocus />
        </EditText>

        <TextView
            android:id="@+id/billTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="19dp"
            android:layout_marginLeft="19dp"
            android:layout_marginTop="20dp"
            android:text="@string/bill_text_view" />

        <TextView
            android:id="@+id/tipTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/BillEditText"
            android:layout_marginStart="34dp"
            android:layout_marginLeft="34dp"
            android:layout_toEndOf="@+id/billTextView"
            android:layout_toRightOf="@+id/BillEditText"
            android:text="@string/tip_text_view" />

        <Button
            android:id="@+id/incrementButton"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignEnd="@+id/tipEditText"
            android:layout_alignRight="@+id/tipEditText"
            android:layout_alignTop="@+id/tipTextView"
            android:onClick="increaaseTip"
            android:text="@string/increment"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/totalTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tipEditText"
            android:layout_toEndOf="@+id/billTextView"
            android:layout_toRightOf="@+id/billTextView"
            android:text="@string/total_text_view" />

        <EditText
            android:id="@+id/totalEditText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/totalTextView"
            android:layout_marginTop="19dp"
            android:layout_toStartOf="@+id/tipTextView"
            android:layout_toLeftOf="@+id/tipTextView"
            android:ems="5"
            android:inputType="numberDecimal"
            android:text="@string/total_edit_text" />

        <EditText
            android:id="@+id/tipEditText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/incrementButton"
            android:layout_marginTop="20dp"
            android:layout_toEndOf="@+id/billTextView"
            android:layout_toRightOf="@+id/tipTextView"
            android:ems="4"
            android:inputType="numberDecimal"
            android:text="@string/tip_edit_text" />

        <Button
            android:id="@+id/decrementButton"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_above="@+id/tipEditText"
            android:layout_marginEnd="12dp"
            android:layout_marginRight="12dp"
            android:layout_toLeftOf="@+id/incrementButton"
            android:layout_toStartOf="@+id/incrementButton"
            android:text="@string/decrement"
            android:textSize="12sp" 
            android:onClick="DecreaseTip"/>

        <SeekBar
            android:id="@+id/changeTipSeekBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/billTextView"
            android:layout_alignRight="@+id/BillEditText"
            android:layout_alignTop="@+id/tipEditText"
            android:progress="15" />

        <TextView
            android:id="@+id/changeTipTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/changeTipSeekBar"
            android:layout_alignLeft="@+id/changeTipSeekBar"
            android:text="@string/change_tip_text_view" />

    </RelativeLayout>

我不确定逻辑如何让我的增量/减量按钮增加和减少小费值

package com.paulk.tipcalc;

import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;


public class TipCalc extends ActionBarActivity{


public final static String BILL_WITHOUT_TIP = "BILL_WITHOUT_TIP";
public final static String CURRENT_TIP = "CURRENT_TIP";
public final static String TOTAL_BILL = "TOTAL_BILL";


private double billBeforeTip;
private double tipAmount;
private double totalAmount;
//private double billAfterTip = (billBeforeTip * tipAmount);

EditText billBeforeTipET;
EditText tipAmountET;
EditText totalAmountET;

Button increment;
Button decrement;

double counter;

SeekBar tipSeekBar;


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

    //if its a new session
    if(savedInstanceState == null){
        billBeforeTip = 0.0;
        tipAmount = 0.10;
        totalAmount = 0.0;

        //if its a saved session
    }else{
        billBeforeTip = savedInstanceState.getDouble(BILL_WITHOUT_TIP);
        tipAmount = savedInstanceState.getDouble(CURRENT_TIP);
        totalAmount = savedInstanceState.getDouble(TOTAL_BILL);
    }

    //pulling through each of the text fields
    billBeforeTipET = (EditText) findViewById(R.id.BillEditText);
    tipAmountET = (EditText) findViewById(R.id.tipEditText);
    totalAmountET = (EditText) findViewById(R.id.totalEditText);

    tipSeekBar = (SeekBar) findViewById(R.id.changeTipSeekBar);

    increment = (Button) findViewById(R.id.incrementButton);
    decrement = (Button) findViewById(R.id.decrementButton);


    billBeforeTipET.addTextChangedListener(billBeforeTipListener);
    tipAmountET.addTextChangedListener(tipAmountListener);

    tipSeekBar.setOnSeekBarChangeListener(tipSeekBarChangeListener);

    /*increment.setOnClickListener(this);
    decrement.setOnClickListener(this);*/




}
private TextWatcher tipAmountListener = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        try{
            tipAmount = Double.parseDouble(s.toString());
        }catch(NumberFormatException e){
            tipAmount = .0;
        }
        updateTipandTotalAmount();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
};
private TextWatcher billBeforeTipListener = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //making sure the correct format is input into the field
        try{
            billBeforeTip = Double.parseDouble(s.toString());
        }catch(NumberFormatException e){
            billBeforeTip = 0.0;
        }
        updateTipandTotalAmount();

    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
};

public void increaseTip(View view){
    if(view.getId() == R.id.incrementButton){

    }
}

我已对此进行了注释,因为我不确定这是为按钮创建逻辑的最佳方法

/*public void onClick(View view){
        if(increment.){

            tipAmount = 
            tipAmount = tipAmount * 0.01;
            tipAmount + 0.01;
            //double tipAmount = Double.parseDouble(tipAmountET.getText().toString());
            tipAmountET.setText(String.format("%.02f", tipAmount));

        }else if(view == decrement){

        }
    }*/

private void updateTipandTotalAmount(){
    //converts the double value into a string value
    double tipAmount = Double.parseDouble(tipAmountET.getText().toString());
    //how the total amount is calculated
    double totalAmount = billBeforeTip + (billBeforeTip * tipAmount);
    //formating the total amount
    totalAmountET.setText(String.format("%.02f", totalAmount));
}
protected void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putDouble(BILL_WITHOUT_TIP, billBeforeTip);
    outState.putDouble(CURRENT_TIP, tipAmount);
    outState.putDouble(TOTAL_BILL, totalAmount);

}

private OnSeekBarChangeListener tipSeekBarChangeListener =  new OnSeekBarChangeListener() {

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
        tipAmount = (tipSeekBar.getProgress()*.01);

        tipAmountET.setText(String.format("%.02f", tipAmount));
    }
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.tip_calc, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

最佳答案

您可以通过设置按钮的监听器来增加或减少使用按钮的计数器。

increment.setOnclickListener();

现在重写 onClick() 方法并检查单击了哪个按钮并编写所需的代码。

 @override
    public void onClick(View view){
        if(view.getId==R.id.incrementButton){

            tipAmount = 
            tipAmount = tipAmount * 0.01;
            tipAmount + 0.01;
            //double tipAmount = Double.parseDouble(tipAmountET.getText().toString());
            tipAmountET.setText(String.format("%.02f", tipAmount));

        }
     if(view.getId==R.id.decrementButton){
      //decrement code.
        }
    }

关于java - 我将如何实现使用 Android 中的按钮按下来增加 double 值的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26594852/

相关文章:

java - 使用元数据唯一标识文件

java - 声明处理程序错误

JAVA GUI按钮问题

html - 如何在一行中水平对齐 css/html 中的按钮?

java - 为什么会有带有 .1 .2 .3 扩展名的 logback 日志文件?

java - 递归查找二维矩阵中的元素

java - WebSphere MQ 是 WebSphere AS 8/8.5 中的默认 JMS 提供程序吗?

android - 删除多个表 execSQL

java - Android:将 TextView 作为参数传递给外部函数返回 nullpointerException

java - onClick 监听器和 onClick on XML 将不起作用