Java tutorial
/* Copyright 2016 Pierre-Yves Lapersonne (aka. "pylapp", pylapp(dot)pylapp(at)gmail(dot)com) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // / ? package aleajactatest.appiumcalculator; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import aleajactatest.appiumcalculator.core.Calculator; import aleajactatest.appiumcalculator.core.Operation; /** * Fragment embedding widgets for a VERY BASIC AND DUMB calculator application. * It possesses a displayed field with buttons. * The computing part is in this fragment too. * * @author pylapp * @version 2.0.0 * @since 29/12/2015 */ public final class CalculatorFragment extends Fragment { /* ********** * * ATTRIBUTES * * ********** */ /** * The score */ private double mScore; /** * The first value */ private int mLeftPart; /** * The second value */ private int mRightPart; /** * The operation */ private Operation mOperation; /** * */ private boolean mIsNewCompute; /** * The core object which makes the computing operations */ private Calculator mCalculator; /* ****************** * * INHERITED METHODS * * ****************** */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); return inflater.inflate(R.layout.activity_calculator, container, false); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mIsNewCompute = true; mCalculator = new Calculator(); initDefaultValues(); initButtonsClickListeners(); } /* ************* * * OTHER METHODS * * ************* */ /** * Cleans the GUI and resets inner values */ public void initDefaultValues() { mLeftPart = 0; mRightPart = 0; mScore = Double.NaN; mOperation = Operation.NOPE; TextView tvScore = (TextView) getView().findViewById(R.id.tvScore); tvScore.setText(""); TextView tvLog = (TextView) getView().findViewById(R.id.tvLog); tvLog.setText(""); } /** * */ public void compute() { mScore = mCalculator.compute(mLeftPart, mOperation, mRightPart); TextView tvScore = (TextView) getView().findViewById(R.id.tvScore); tvScore.setText(Double.toString(mScore)); } /** * What should it do from your point of view? * @param help - A message to display */ public void showHelp(String help) { if (help == null || help.length() <= 0 || (help.length() > 0 && help.trim().length() <= 0)) throw new IllegalArgumentException("What's that string ?"); Toast.makeText(getActivity(), help, Toast.LENGTH_LONG).show(); } /** * What should it do from your point of view? * No controls are done. * @param help - A message to display */ public void showHelpDummy(String help) { Toast.makeText(getActivity(), help, Toast.LENGTH_LONG).show(); } /** * Initializes the click listeners on the buttons */ private void initButtonsClickListeners() { View.OnClickListener globalClickListener = new View.OnClickListener() { @Override public void onClick(View v) { try { if (v != null && !(v instanceof Button)) return; if (mIsNewCompute) { initDefaultValues(); mIsNewCompute = false; } TextView tvScore = (TextView) getActivity().findViewById(R.id.tvScore); Button b = (Button) v; String val = b.getText().toString(); // Updates the display field String text = tvScore.getText() + ""; tvScore.setText(text + val); // ... switch (val) { case "0": case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9": if (mOperation == Operation.NOPE) { mLeftPart = Integer.parseInt(val); } else { mRightPart = Integer.parseInt(val); } break; case "+": mOperation = Operation.ADD; break; case "-": mOperation = Operation.SUB; break; case "/": mOperation = Operation.DIV; break; case "X": mOperation = Operation.MULT; break; case "=": mScore = mCalculator.compute(mLeftPart, mOperation, mRightPart); tvScore.setText(Double.toString(mScore)); mIsNewCompute = true; break; case "Clean": default: initDefaultValues(); break; } } catch (Exception e) { Log.e("CalculatorActivity", e.getMessage()); TextView tvLog = (TextView) getActivity().findViewById(R.id.tvLog); tvLog.setText(e.getMessage()); } } }; View parent = getView(); parent.findViewById(R.id.button0).setOnClickListener(globalClickListener); parent.findViewById(R.id.button1).setOnClickListener(globalClickListener); parent.findViewById(R.id.button2).setOnClickListener(globalClickListener); parent.findViewById(R.id.button3).setOnClickListener(globalClickListener); parent.findViewById(R.id.button0).setOnClickListener(globalClickListener); parent.findViewById(R.id.button5).setOnClickListener(globalClickListener); parent.findViewById(R.id.button6).setOnClickListener(globalClickListener); parent.findViewById(R.id.button7).setOnClickListener(globalClickListener); parent.findViewById(R.id.button8).setOnClickListener(globalClickListener); parent.findViewById(R.id.button9).setOnClickListener(globalClickListener); parent.findViewById(R.id.buttonAdd).setOnClickListener(globalClickListener); parent.findViewById(R.id.buttonDiv).setOnClickListener(globalClickListener); parent.findViewById(R.id.buttonSub).setOnClickListener(globalClickListener); parent.findViewById(R.id.buttonMul).setOnClickListener(globalClickListener); parent.findViewById(R.id.buttonEqual).setOnClickListener(globalClickListener); parent.findViewById(R.id.buttonClean).setOnClickListener(globalClickListener); } }