当前位置: 代码迷 >> Android >> 有个有关问题不懂,求指点
  详细解决方案

有个有关问题不懂,求指点

热度:110   发布时间:2016-04-27 22:34:30.0
有个问题不懂,求指点
今天看完书自己写了代码,但是在Bill Total这个点上遇到了问题,求助
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) {

        }
    };
}

------解决思路----------------------
假如你是直接获取输入框数值但数值不对的话可能是findviewbyid绑定错控件了,还有就是用的eclipse改过布局文件,有时候改过两个控件位置的话会出现这种情况,把其中一个注释掉运行然后再解除注释就好了
  相关解决方案