public class T_Calculator extends Activity{
private static final String BILL_TOTAL = "BILL_TOTAL";
private static final String CUSTOM_PERCENT = "CUSTOM_PERCENT";
private double currentBillTotal;
private int currentCustomPercent;
private EditText Tip10_et;
private EditText Tip15_et;
private EditText Tip20_et;
private EditText Total10_et;
private EditText Total15_et;
private EditText Total20_et;
private EditText Bt_et;
private TextView CustomTip_tv;
private EditText TipCustom_et;
private EditText TotalCustom_et;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_t__calculator);
//
if (savedInstanceState == null)
{
currentBillTotal = 0.0;
currentCustomPercent = 18;
}
else {
currentBillTotal = savedInstanceState.getDouble(BILL_TOTAL);
currentCustomPercent = savedInstanceState.getInt(CUSTOM_PERCENT);
}
Tip10_et = (EditText) findViewById(R.id.Tip10_et);
Total10_et = (EditText)findViewById(R.id.Total10_et);
Tip15_et = (EditText)findViewById(R.id.Tip15_et);
Total15_et = (EditText)findViewById(R.id.Total15_et);
Tip20_et = (EditText)findViewById(R.id.Tip20_et);
Total20_et = (EditText)findViewById(R.id.Total20_et);
CustomTip_tv = (TextView)findViewById(R.id.CustomTip_tv);
TipCustom_et = (EditText) findViewById(R.id.TipCustom_et);
TotalCustom_et = (EditText) findViewById(R.id.TotalCustom_et);
Bt_et = (EditText) findViewById(R.id.Bt_et);
Bt_et.addTextChangedListener(Bt_etWatcher);
SeekBar Custom_Sb = (SeekBar) findViewById(R.id.Custom_Sb);
Custom_Sb.setOnSeekBarChangeListener(Custom_SbListener);
}
public void updateStandard(){
Double tenPercentTip = currentBillTotal * .1;
Double tenPercentTotal = currentBillTotal + tenPercentTip;
//
Tip10_et.setText(String.format("%.02f", tenPercentTip));
Total10_et.setText(String.format("%.02f", tenPercentTotal));
//
Double fifteenPercentTip = currentBillTotal * .15;
Double fifteenPercentTotal = currentBillTotal + fifteenPercentTip;
Tip15_et.setText(String.format("%.02f", fifteenPercentTip));
Total15_et.setText(String.format("%.02f", fifteenPercentTotal));
//
Double twentyPercentTip = currentBillTotal * .20;
Double twentyPercentTotal = currentBillTotal + twentyPercentTip;
Tip20_et.setText(String.format("%.02f", twentyPercentTip));
Total20_et.setText(String.format("%.02f", twentyPercentTotal));
}
public void updateCustom(){
CustomTip_tv.setText(currentCustomPercent + "%");
Double customTipAmount = currentBillTotal * currentCustomPercent * .01;
Double customTotalAmount = currentBillTotal + customTipAmount;
TipCustom_et.setText(String.format("%.02f", customTipAmount));
TotalCustom_et.setText(String.format("%.02f", customTotalAmount));
}
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putDouble(BILL_TOTAL , currentBillTotal);
outState.putInt(CUSTOM_PERCENT , currentCustomPercent);
}
private SeekBar.OnSeekBarChangeListener Custom_SbListener =
new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
currentCustomPercent = seekBar.getProgress();
updateCustom();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
private TextWatcher Bt_etWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
try {
currentBillTotal = Double.parseDouble(s.toString());
}
catch (NumberFormatException e){
currentBillTotal = 0.0;
}
updateCustom();
updateStandard();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
}
apue.h包頭找不到的問題
----------------------biu~biu~biu~~~在下問答機器人小D,這是我依靠自己的聰明才智給出的答案,如果不正確,你來咬我啊!