应用程序执行double + double或double + int

时间:2018-03-03 19:26:51

标签: java android double decimal

所以我有一个应用程序,我想让人们能够在第一个框中输入fx 1.1,在第二个框中输入2个soi将加上两个框,它也支持1个小数但是它给出了错误的答案并且只能工作第一次(见视频)youtu.be/vawL_bFBV-k 已经尝试了很多东西,但它不起作用

这是代码

package me.danilkp1234.laerkeholtmilk;

import android.content.Context;
import android.content.SharedPreferences;
import android.icu.text.IDNA;
import android.os.Debug;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.crashlytics.android.Crashlytics;
import com.google.firebase.crash.FirebaseCrash;

import static me.danilkp1234.laerkeholtmilk.R.id.brix;
import static me.danilkp1234.laerkeholtmilk.R.id.textView;
import static me.danilkp1234.laerkeholtmilk.R.id.textView3;
public class MainActivity extends AppCompatActivity {

private EditText milkdone;
private EditText brixdone;
private double milkdone2;
private double brixdone2;
private double milkdone3;
private double brixdone3;
SharedPreferences.Editor putDouble(final SharedPreferences.Editor edit, final String key, final double value) {
    return edit.putLong(key, Double.doubleToRawLongBits(value));
}
double getDouble(final SharedPreferences prefs, final String key, final double defaultValue) {
    try{
        return Double.longBitsToDouble(prefs.getLong(key,   Double.doubleToLongBits(defaultValue)));
    }catch( ClassCastException e ){
        // The pref could be a String, if that is the case try this
        // recovery bit
        try{
            String value = prefs.getString(key,""+defaultValue);
            double result = Double.parseDouble(value);
            return result ;
        }catch( Exception e2){
            // our  recovery bit failed. The problem is elsewhere. Send the original error
            throw e ;
        }
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    milkdone = (EditText) findViewById(R.id.milkliter);
    brixdone = (EditText) findViewById(R.id.brix);
    SharedPreferences prefs = getSharedPreferences("SavedTotals",Context.MODE_PRIVATE);
    Double MilkDone2Saved=(getDouble(prefs,"MilkDone2Saved",milkdone2));
    FirebaseCrash.log(String.valueOf(MilkDone2Saved));
    FirebaseCrash.log(String.valueOf(milkdone2));
    Double BrixDone2Saved=(getDouble(prefs,"BrixDone2Saved",brixdone2));
    milkdone2 = MilkDone2Saved;
    brixdone2 = BrixDone2Saved;
    FirebaseCrash.log(String.valueOf(MilkDone2Saved));
    FirebaseCrash.log(String.valueOf(milkdone2));
    brixdone.setText(String.valueOf(brixdone2));
    milkdone.setText(String.valueOf(milkdone2));
    milkdone.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                SharedPreferences prefs = getSharedPreferences("SavedTotals",Context.MODE_PRIVATE);
                Double MilkDone2Saved=(getDouble(prefs,"MilkDone2Saved",milkdone2));
                Double BrixDone2Saved=(getDouble(prefs,"BrixDone2Saved",brixdone2));
                FirebaseCrash.log(String.valueOf(MilkDone2Saved));
                FirebaseCrash.log(String.valueOf(milkdone2));
                milkdone3 = MilkDone2Saved;
                brixdone3 = BrixDone2Saved;
                FirebaseCrash.log(String.valueOf(MilkDone2Saved));
                FirebaseCrash.log(String.valueOf(milkdone2));
                double test2 = brixdone3 + milkdone3;
                TextView change = (TextView) findViewById(R.id.textView3);
                change.setText(String.valueOf(test2));
                handled = true;
            }
            return handled;
        }

    });
    brixdone.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_DONE){
                SharedPreferences prefs = getSharedPreferences("SavedTotals",Context.MODE_PRIVATE);
                Double MilkDone2Saved=(getDouble(prefs,"MilkDone2Saved",milkdone2));
                Double BrixDone2Saved=(getDouble(prefs,"BrixDone2Saved",brixdone2));
                milkdone3 = MilkDone2Saved;
                brixdone3 = BrixDone2Saved;
                double test2 = brixdone3 + milkdone3;
                System.out.println(test2);
                Log.d("TEST",String.valueOf(test2));
                TextView change = (TextView) findViewById(R.id.textView3);
                change.setText(String.valueOf(test2));
                handled = true;
            }
            return handled;
        }
    });
    };
    public void onStop() {
        super.onStop();
        storeDouble("MilkDone2Saved",milkdone2);
        storeDouble("BrixDone2Saved",brixdone2);

    }
    public void storeDouble(String d, Double c){
        SharedPreferences prefs = getSharedPreferences("SavedTotals",Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = prefs.edit();

        putDouble(editor,d,c);
        editor.commit();
}

}

如果你想要完整的项目,请点击此处github.com/danielkp1234/Laerkeholtmilk

过去几个月我一直试图解决这个问题

TLDR: 它没有正确的数学运算 它只适用于第一个

希望你们能帮忙

0 个答案:

没有答案