Android入門計算器編寫代碼
這個簡易計算器是我按照一本android開發(fā)入門書學(xué)的,書上的第一個例子就是計算器的編寫。計算器的編寫主要涉及到按鍵的布局和按鍵輸入要點。
一個總的Lnearlayout的布局下orientation設(shè)置為vertical垂直分布,然后此布局下再設(shè)置1給我Edittext的一個文本框4個Lnearlayout子布局(第4個布局里可以嵌套另外3個Lnearlayout的布局來實現(xiàn)按鈕排版)這4個子布局在你的界面上肯定是垂直分布的,因為你的總布局設(shè)置vertical。第一個子布局放置4個Button,分別是mc、m+、m-和mr這4個功能按鈕。
布局代碼就不貼了,貼下加減乘除的代碼。
package com.example.boss.calculator; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, btn_equal, btn_point, btn_clean, btn_del, btn_plus, btn_minus, btn_multiply, btn_divide; EditText et_input; boolean clear_flag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_0 = (Button) findViewById(R.id.btn_0); btn_1 = (Button) findViewById(R.id.btn_1); btn_2 = (Button) findViewById(R.id.btn_2); btn_3 = (Button) findViewById(R.id.btn_3); btn_4 = (Button) findViewById(R.id.btn_4); btn_5 = (Button) findViewById(R.id.btn_5); btn_6 = (Button) findViewById(R.id.btn_6); btn_7 = (Button) findViewById(R.id.btn_7); btn_8 = (Button) findViewById(R.id.btn_8); btn_9 = (Button) findViewById(R.id.btn_9); btn_clean = (Button) findViewById(R.id.btn_clean); btn_equal = (Button) findViewById(R.id.btn_equal); btn_minus = (Button) findViewById(R.id.btn_minus); btn_multiply = (Button) findViewById(R.id.btn_multiplay); btn_plus = (Button) findViewById(R.id.btn_plus); btn_point = (Button) findViewById(R.id.btn_point); btn_del = (Button) findViewById(R.id.btn_del); btn_divide = (Button) findViewById(R.id.btn_divide); et_input = (EditText) findViewById(R.id.et_input); btn_0.setOnClickListener(this); btn_1.setOnClickListener(this); btn_2.setOnClickListener(this); btn_3.setOnClickListener(this); btn_4.setOnClickListener(this); btn_5.setOnClickListener(this); btn_6.setOnClickListener(this); btn_7.setOnClickListener(this); btn_8.setOnClickListener(this); btn_9.setOnClickListener(this); btn_equal.setOnClickListener(this); btn_minus.setOnClickListener(this); btn_multiply.setOnClickListener(this); btn_divide.setOnClickListener(this); btn_del.setOnClickListener(this); btn_point.setOnClickListener(this); btn_plus.setOnClickListener(this); btn_clean.setOnClickListener(this); et_input.setOnClickListener(this); } @Override public void onClick(View v) { String str = et_input.getText().toString(); switch (v.getId()) { case R.id.btn_0: case R.id.btn_1: case R.id.btn_2: case R.id.btn_3: case R.id.btn_4: case R.id.btn_5: case R.id.btn_6: case R.id.btn_7: case R.id.btn_8: case R.id.btn_9: case R.id.btn_point: if (clear_flag) { clear_flag = false; str = ""; et_input.setText(""); } et_input.setText(str + ((Button) v).getText()); break; case R.id.btn_plus: case R.id.btn_minus: case R.id.btn_multiplay: case R.id.btn_divide: if (clear_flag) { clear_flag = false; str = ""; et_input.setText(""); } et_input.setText(str + " " + ((Button) v).getText() + " "); break; case R.id.btn_clean: clear_flag = false; et_input.setText(""); break; case R.id.btn_del: if (clear_flag) { clear_flag = false; et_input.setText(""); } else if (str != null && !str.equals("")) { et_input.setText(str.substring(0, str.length() - 1)); } case R.id.btn_equal: getResult(); break; default: break; } } private void getResult() { String exp = et_input.getText().toString(); if(exp==null||exp.equals("")){ return; } if(!exp.contains(" ")){ return; } if(clear_flag){ clear_flag=false; return ; } clear_flag = true; double result = 0; String s1 = exp.substring(0, exp.indexOf(" ")); String op = exp.substring(exp.indexOf(" ") + 1, exp.indexOf(" ") + 2); String s2 = exp.substring(exp.indexOf(" ") + 3); if (!s1.equals("") && !s2.equals("")) { double d1 = Double.parseDouble(s1); double d2 = Double.parseDouble(s2); if (op.equals("+")) { result = d1 + d2; } else if (op.equals("-")) { result = d1 - d2; } else if (op.equals("*")) { result = d1 * d2; } else if (op.equals("/")) { if (d2 == 0) { result = 0; } else { result = d1 / d2; } } if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) { int r = (int) result; et_input.setText(r + ""); } else { et_input.setText(result + ""); } } else if(!s1.equals("")&&s2.equals("")){ et_input.setText(exp); } else if(s1.equals("")&&!s2.equals("")){ double d2=Double.parseDouble(s2); if(op.equals("+")){ result=0+d2; }else if(op.equals("-")){ result=0-d2; }else if(op.equals("*")){ result=0; }else if(op.equals("/")){ result = 0; } if(!s2.contains(".")){ int r=(int) result; et_input.setText(r+" "); } else{ et_input.setText(result+" "); } }else{ et_input.setText(""); } } }
更多計算器功能實現(xiàn),請點擊專題: 計算器功能匯總 進(jìn)行學(xué)習(xí)
關(guān)于Android計算器功能的實現(xiàn),查看專題:Android計算器 進(jìn)行學(xué)習(xí)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android studio實現(xiàn)簡單的計算器(無bug)
- android簡易計算器的制作
- android studio實現(xiàn)簡單的計算器功能
- Android開發(fā)學(xué)習(xí)實現(xiàn)簡單計算器
- 從零開始學(xué)android實現(xiàn)計算器功能示例分享(計算器源碼)
- Android開發(fā)實現(xiàn)的簡單計算器功能【附完整demo源碼下載】
- android計算器簡單實現(xiàn)代碼
- Android計算器編寫代碼
- android計時器,時間計算器的實現(xiàn)方法
- Android實現(xiàn)計算器(計算表達(dá)式/計算小數(shù)點以及括號)
相關(guān)文章
android中RecyclerView自定義分割線實現(xiàn)
本篇文章主要介紹了android中RecyclerView自定義分割線實現(xiàn),由于RecyclerView的布局方式多種多樣,所以它的分割線也根據(jù)布局的不同有所差異,本文只針對LinearLayoutManager線性布局。2017-03-03Android使用Pull解析器解析xml文件的實現(xiàn)代碼
Android使用Pull解析器解析xml文件的實現(xiàn)代碼,需要的朋友可以參考一下2013-02-02利用Android畫圓弧canvas.drawArc()實例詳解
這篇文章主要給大家介紹了關(guān)于利用Android畫圓弧canvas.drawArc()的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的理解和學(xué)習(xí)具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11