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 realize simple Calculator Design with Android Studio

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you how Android Studio to achieve simple calculator design related knowledge, detailed content, clear logic, I believe that most people are too aware of this knowledge, so share this article for your reference, I hope you will learn something after reading this article, let's take a look at it.

I. title

1. As shown in the figure (actual design, similar to this interface style, when full screen, the button will stretch longitudinally), use grid layout manager to design a center, full screen calculator, project name: clc666b; (666, change to your own actual number)

2. Add and multiply are selected by 2 radio buttons respectively

3. Write a program for clc666b (clc666a does not need programming, only design the interface), and realize the simple calculation of the addition and multiplication of two numbers according to the selected addition (multiplication) radio button.

4. In order to simplify the program design, the upper data area can also be designed into three text boxes (better if one text box functions), which can be used as addition, addition and product, respectively.

2. Analysis

1. The primary goal is to make a window first, and the window design needs to be divided equally, so modify the weight of each part.

2.java programming, to monitor different kinds of buttons, the Internet is basically ordinary button programs, there is no radiobutton, this topic for me this novice is a bit unfriendly, can not directly copy the network, but also according to the teacher said in class to change.

(1) when the number key is pressed, the number corresponding to the key is saved in a string, and then the text is updated.

(2) if you press delete, delete the last character of the string, and then update text.

(3) when the Operand key is pressed, the preceding character is stored in a string an and the id address of the Operand key is saved.

(4) continue with the first two steps until you press the equals key to run (5).

(5) give the symbol after the operation to the string b, operate on an and b according to id, and update text.

III. Code

1. Updated xml code

two。 Updated java code

Package com.example.lenovo.clc231b; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle;import android.view.View;import android.widget.Button;import android.view.View.OnClickListener;import android.widget.TextView; public class MainActivity extends AppCompatActivity {private String num = ""; private String num_zong = ""; private int fore,back,lenth,id; TextView textview1; Button nm1; Button nm2; Button nm3; Button del; Button equ; Button mul Button add; @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); textview1= (TextView) findViewById (R.id.textview1); nm1 = (Button) findViewById (R.id.nm1); nm2 = (Button) findViewById (R.id.nm2); nm3 = (Button) findViewById (R.id.nm3) Del = (Button) findViewById (R.id.del); equ = (Button) findViewById (R.id.equ); mul = (Button) findViewById (R.id.mul); add = (Button) findViewById (R.id.add); nm1.setOnClickListener (listener); nm2.setOnClickListener (listener); nm3.setOnClickListener (listener); del.setOnClickListener (listener); equ.setOnClickListener (listener) Mul.setOnClickListener (listener); add.setOnClickListener (listener);} / / listen button public OnClickListener listener = new OnClickListener () {@ Override public void onClick (View view) {switch (view.getId ()) {case R.id.nm1: number (1); break Case R.id.nm2: number (2); break; case R.id.nm3: number (3); break; case R.id.del: delete (); break Case R.id.equ: result (); break; case R.id.mul: Get_mul_add (1); break; case R.id.add: Get_mul_add (2) Break; default: break;} textview1.setText (num);}}; private void Get_mul_add (int flag) {fore = Integer.valueOf (num); if (flag = = 1) {id = R.id.mule; num + = "×" } else {id = R.id.add. num + = "+";} textview1.setText (num); lenth = num.length ();} private void result () {num_zong = num; num = num.substring (lenth); back = Integer.valueOf (num) If (id = = R.id.mul) num = String.valueOf ((fore*back)); else num = String.valueOf ((fore+back)); num = num_zong + "=" + num;} private void delete () {num = num.substring (0mum. Length ()-1);} private void number (int I) {num + = I;}}

3. The original code

Package com.example.lenovo.clc231b; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle;import android.view.View;import android.widget.Button;import android.view.View.OnClickListener;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView; public class MainActivity extends AppCompatActivity {private String num = ""; private String num_zong = ""; private int fore,back,lenth,id; TextView textview1; RadioGroup question2; Button button1; Button button2; Button button3 Button button4; Button button5; @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); textview1= (TextView) findViewById (R.id.textview1); button1 = (Button) findViewById (R.id.button1); button2 = (Button) findViewById (R.id.button2); button3 = (Button) findViewById (R.id.button3) Button4 = (Button) findViewById (R.id.button4); button5 = (Button) findViewById (R.id.button5); question2 = (RadioGroup) findViewById (R.id.radioGroup2); button1.setOnClickListener (listener); button2.setOnClickListener (listener); button3.setOnClickListener (listener); button4.setOnClickListener (listener); button5.setOnClickListener (listener); question2.setOnClickListener (listener) } public OnClickListener listener = new OnClickListener () {@ Override public void onClick (View view) {switch (view.getId ()) {case R.id.button1: number (1); break; case R.id.button2: number (2) Break; case R.id.button3: number (3); break; case R.id.button4: delete (); break; case R.id.button5: result () Break; default: break;} question2.setOnCheckedChangeListener (new RadioGroup.OnCheckedChangeListener () {@ Override public void onCheckedChanged (RadioGroup group, int checkedId) {/ / get the selected radio button RadioButton r = (RadioButton) findViewById (checkedId) / / Toast.makeText (MainActivity.this, "your hobby is:" + r.getText (), Toast.LENGTH_SHORT). Show (); id = r.getId (); textview1.setText (num+ r.getText ()); fore = Integer.valueOf (num); num = num+ r.getText () Lenth = num.length (); textview1.setText (num);}}; / / the multiplication sign id is 2131165311 plus sign id is 2131165312 private void result () {num_zong = num; num = num.substring (lenth); back = Integer.valueOf (num) If (id = = 2131165311) num = String.valueOf ((fore*back)); else num = String.valueOf ((fore+back)); num = num_zong + "=" + num;} private void delete () {num = num.substring (0num.length ()-1);} private void number (int I) {num = num + I }} these are all the contents of the article "how to realize the Design of simple Calculator in Android Studio". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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