How to realize Calculator function in Android Development
Today, I would like to share with you the relevant knowledge of how to achieve calculator functions in Android development. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
Let's see the effect of the picture above.
It's relatively simple, so I didn't write much notes. I should be able to understand it at a glance.
If you don't understand, you can write to me and ask me.
Paste the MainActivity.java code first
Package com.example.calculator;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, bp, bs, bm, bd, bc, be; ImageView delete TextView tv; EditText show; String showString = "", option = ""; int showfirst = 0; String exception = ""; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); b0 = (Button) findViewById (R.id.bt_0); b1 = (Button) findViewById (R.id.bt_1); b2 = (Button) findViewById (R.id.bt_2) B3 = (Button) findViewById (R.id.bt_3); b4 = (Button) findViewById (R.id.bt_4); b5 = (Button) findViewById (R.id.bt_5); b6 = (Button) findViewById (R.id.bt_6); b7 = (Button) findViewById (R.id.bt_7); B8 = (Button) findViewById (R.id.bt_8); b9 = (Button) findViewById (R.id.bt_9) Bp = (Button) findViewById (R.id.bt_plus); bs = (Button) findViewById (R.id.bt_sub); bm = (Button) findViewById (R.id.bt_mutilate); bd = (Button) findViewById (R.id.bt_div); bc = (Button) findViewById (R.id.bt_c); be = (Button) findViewById (R.id.bt_equ); b1.setOnClickListener (this); b2.setOnClickListener (this) B3.setOnClickListener (this); b4.setOnClickListener (this); b5.setOnClickListener (this); b6.setOnClickListener (this); b7.setOnClickListener (this); b8.setOnClickListener (this); b9.setOnClickListener (this); b0.setOnClickListener (this); bp.setOnClickListener (this); bs.setOnClickListener (this); bm.setOnClickListener (this); bd.setOnClickListener (this); bc.setOnClickListener (this); be.setOnClickListener (this) Show = (EditText) findViewById (R.id.et_show); delete = (ImageView) findViewById (R.id.iv_delete); delete.setOnClickListener (this); tv= (TextView) findViewById (R.id.author); tv.setOnClickListener (this);} @ Override public boolean onCreateOptionsMenu (Menu menu) {/ / Inflate the menu; this adds items to the action bar if it is present. GetMenuInflater () .inflate (R.menu.main, menu); return true;} @ Override public boolean onOptionsItemSelected (MenuItem item) {/ / Handle action bar item clicks here. The action bar will / / automatically handle clicks on the Home/Up button, so long / / as you specify a parent activity in AndroidManifest.xml. Int id = item.getItemId (); if (id = = R.id.action_settings) {return true;} return super.onOptionsItemSelected (item);} @ Override public void onClick (View v) {switch (v.getId ()) {case R.id.bt_0: showString + = "0"; break; case R.id.bt_1: showString + = "1"; break Case R.id.bt_2: showString + = "2"; break; case R.id.bt_3: showString + = "3"; break; case R.id.bt_4: showString + = "4"; break; case R.id.bt_5: showString + = "5"; break; case R.id.bt_6: showString + = "6"; break Case R.id.bt_7: showString + = "7"; break; case R.id.bt_8: showString + = "8"; break; case R.id.bt_9: showString + = "9"; break; case R.id.bt_plus: if (showString.equals (")) exception =" enter numeric value first "; else {showfirst = Integer.parseInt (showString) ShowString = ""; option = "+";} break; case R.id.bt_sub: if (showString.equals (")) exception =" enter the value first "; else {showfirst = Integer.parseInt (showString); showString ="; option ="-";} break Case R.id.bt_mutilate: if (showString.equals (")) exception =" enter numerical value first "; else {showfirst = Integer.parseInt (showString); showString ="; option =" * ";} break; case R.id.bt_div: if (showString.equals (")) exception = "enter numerical value first" Else {showfirst = Integer.parseInt (showString); showString = ""; option = "/";} break; case R.id.bt_equ: if (option.equals ("+")) showString = showfirst + Integer.parseInt (showString) + "; else if (option.equals ("-")) {showString = showfirst-Integer.parseInt (showString) +" } else if (option.equals ("*")) {showString = showfirst * Integer.parseInt (showString) + ";} else if (option.equals (" / ")) {if (showString.equals (" 0 ")) {exception =" Divisor cannot be 0! " ;} else showString = showfirst / Integer.parseInt (showString) + ";} break; case R.id.bt_c: showString ="; break; case R.id.iv_delete: Toast.makeText (MainActivity.this, showString +" emptied ", Toast.LENGTH_SHORT). Show (); showString ="; break Case R.id.author: Toast.makeText (MainActivity.this, "Zheng Liang\ nSoftware Engineering\ nQQ:1072307340", Toast.LENGTH_SHORT). Show (); break; default: break;} if (exception.equals (")) show.setText (showString); else {show.setText (exception); exception =";} / / set text box color If (! show.getText (). ToString (). Equals (")) {delete.setBackgroundColor (R.drawable.delete_gray);} else {delete.setBackgroundResource (R.drawable.delete);}
Repost the layout activity_main.xml:
I also wrote a xml for drawable. See for yourself.
Delete_and_deletegray.xml:
These are all the contents of the article "how to achieve Calculator function in Android Development". 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.