我在运行时遇到问题。它不是按tipAmount
递增.01
,而是从0.10
到0.11
再到0.01
,但我不明白为什么,我似乎无法看到代码或onClick
逻辑有问题。
代码
package com.paulk.tipcalc;
import android.R.integer;
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 implements OnClickListener{
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;
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.getInt(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 = (int) Double.parseDouble(s.toString());
}catch(NumberFormatException e){
tipAmount = 0.10;
}
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
}
};
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 void onClick(View v) {
switch(v.getId()){
case R.id.incrementButton:
tipAmount = tipAmount + 0.01;
tipAmountET.setText(String.format("%.02f", tipAmount));
break;
case R.id.decrementButton:
tipAmount = tipAmount - 0.01;
tipAmountET.setText(String.format("%.02f", tipAmount));
break;
}
}
@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);
}
}
XML
<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>
字符串类
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TipCalc</string>
<string name="action_settings">Settings</string>
<string name="bill_text_view">Bill:</string>
<string name="bill_edit_text"></string>
<string name="decrement">-</string>
<string name="increment">+</string>
<string name="tip_text_view">Tip:</string>
<string name="tip_edit_text">0.10</string>
<string name="total_text_view">Total:</string>
<string name="total_edit_text"></string>
<string name="change_tip_text_view">Tip Changer</string>
</resources>
我只是想要它所以它从0.10开始并递增并递减0.01,但是我尝试了一些事情并没有运气,任何想法为什么?
答案 0 :(得分:1)
您的问题可能是您正在使用getInt
从Bundle
加载双变量:
tipAmount = savedInstanceState.getInt(CURRENT_TIP);
这将返回0,因为您将值保存为double:
outState.putDouble(CURRENT_TIP, tipAmount);
将其更改为:
tipAmount = savedInstanceState.getDouble (CURRENT_TIP);
public int getInt (字符串键)
在API级别1中添加返回与给定键关联的值,如果没有映射,则返回 0 给定键
存在所需类型
另一个问题出在您的onTextChanged
方法中:
tipAmount = (int) Double.parseDouble(s.toString());
如果解析double的结果&lt; 1,tipAmount将通过强制转换为int设置为0。
答案 1 :(得分:1)
在你的textwatcher中:
tipAmount = (int) Double.parseDouble(s.toString());
您将提示金额转换为整数。 (int)0.11
为0
,并向其添加0.01
会再次产生0.01
。