Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report