ปรัชญา : โยคา เว ชายเต ภูริ (ปัญญาย่อมเกิดขึ้นเพราะการฝึกฝน)

ให้เรตสมาชิก: 5 / 5

ดาวใช้งานดาวใช้งานดาวใช้งานดาวใช้งานดาวใช้งาน
 


MainActivity

*******************************************************************************************

    public void mymet(View v) {
        Intent  intent = new Intent(MainActivity.this,SecondActivity.class);
        double input1 = Double.parseDouble(editt.getText().toString());
        double input2 = Double.parseDouble(editt2.getText().toString());
        String oper   = editt3.getText().toString();
        intent.putExtra("input1", input1);
        intent.putExtra("input2", input2);
        intent.putExtra("oper", oper);
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if(requestCode==1) {

            double mdb = data.getDoubleExtra("resultdb",0.00);
            editt4.setText(String.valueOf(mdb));
        }
    }

 

*********************************************************************************************

SecondActivity

***********************************************************************************************

    public void mymet(View v) {
        Intent returnIntent = new Intent();
        double minput1 = getIntent().getDoubleExtra("input1", 0);
        double minput2 = getIntent().getDoubleExtra("input2", 0);
        String moper  = getIntent().getStringExtra("oper");
        double mresultdb=0;
        switch (moper) {
            case "+":
                mresultdb = minput1 + minput2;
                break;
            case "-":
                mresultdb = minput1 - minput2;
                break;
            case "*":
                mresultdb = minput1 * minput2;
                break;
            case "/":
                mresultdb = minput1 / minput2;
        }
        returnIntent.putExtra("resultdb", mresultdb);
        setResult(Activity.RESULT_OK, returnIntent);
        finish();
    }