Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write the code of a simple calculator realized by Android

Shulou Source: shulou.com Published: 2022-06-01 12:31:57 09月25日 Update

This article mainly explains the "Android implementation of simple calculator code how to write", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "Android to achieve simple calculator code how to write" it!

Activity_main.xml

Arrays.xml

Please select the operator +-* /

MainActivity

Package com.jld.homework; import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.Button;import android.widget.EditText;import android.widget.Spinner;import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity {String op; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); Spinner spinner = (Spinner) this.findViewById (R.id.operator) / / get the Spinner object in the active layout / / register the internal listener object spinner.setOnItemSelectedListener (new AdapterView.OnItemSelectedListener () {@ Overridepublic void onItemSelected (AdapterView parent, View view, int position, long id) {/ / assign the value of the Spinner option to the member variable op (save arithmetic operator) op = ((TextView) view). GetText () .toString ();} @ Overridepublic void onNothingSelected (AdapterView parent) {}) / / various components Button calcButton = findViewById (R.id.calc); EditText firstNum = findViewById (R.id.firstNum); EditText secondNum = findViewById (R.id.secondNum); TextView result = findViewById (R.id.result); / / Computing Core calcButton.setOnClickListener (v-> {switch (op) {case "+": {double r = Double.parseDouble (firstNum.getText (). ToString ()) + Double.parseDouble (secondNum.getText (). ToString ()); result.setText (String.valueOf (r)); break } case "-": {double r = Double.parseDouble (firstNum.getText (). ToString ())-Double.parseDouble (secondNum.getText (). ToString ()); result.setText (String.valueOf (r)); break;} case "*": {double r = Double.parseDouble (firstNum.getText (). ToString ()) * Double.parseDouble (secondNum.getText (). ToString ()); result.setText (String.valueOf (r)); break } case "/": {double r = Double.parseDouble (firstNum.getText (). ToString ()) / Double.parseDouble (secondNum.getText (). ToString ()); result.setText (String.valueOf (r)); break;} default:// illegal error result.setText (R.string.ERROR); break;}}) }} Thank you for your reading. The above is the content of "how to write the code for Android to implement a simple calculator". After the study of this article, I believe you have a deeper understanding of how to write the code for Android to achieve a simple calculator, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Code simplicity calculators learning content objects situations operators operations variables that is layout ideas members articles more core listeners knowledge knowledge points Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Redmi Xiaomi MySQL Docker