Android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
功能
1、加減乘除四則運(yùn)算
2、歸0
3、回退
4、即時(shí)運(yùn)算
配置
在build.gradle(app) 中加入下面的代碼
buildFeatures {
viewBinding = true
}
加入位置如下所示
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures {
viewBinding = true
}
布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:layout_margin="5dp"
android:text="計(jì)算器\nby csdn weixin_44423317 "
android:textSize="20sp"
android:gravity="right|center"
android:padding="6dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:background="#ADADAD"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/ac"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="20sp"
android:textColor="@color/black"
android:text="AC"
android:background="@color/white"/>
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#00FFFFFF"/>
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#00FFFFFF"/>
<ImageButton
android:id="@+id/dec"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textSize="20sp"
android:textColor="@color/black"
android:layout_gravity="right"
android:background="@color/white"
android:src="@drawable/ic_baseline_backspace_24"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/qi"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textSize="20sp"
android:textColor="@color/black"
android:text="7"
android:background="@color/white"/>
<Button
android:id="@+id/ba"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textSize="20sp"
android:textColor="@color/black"
android:text="8"
android:background="@color/white"/>
<Button
android:id="@+id/jiu"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textColor="@color/black"
android:text="9"
android:textSize="20sp"
android:background="@color/white"/>
<Button
android:id="@+id/mul"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textSize="20sp"
android:textColor="@color/black"
android:text="x"
android:background="@color/white"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/si"
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="20sp"
android:layout_height="match_parent"
android:textColor="@color/black"
android:text="4"
android:background="@color/white"/>
<Button
android:id="@+id/wu"
android:textSize="20sp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textColor="@color/black"
android:text="5"
android:background="@color/white"/>
<Button
android:id="@+id/liu"
android:textSize="20sp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textColor="@color/black"
android:text="6"
android:background="@color/white"/>
<Button
android:id="@+id/div"
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="20sp"
android:layout_height="match_parent"
android:textColor="@color/black"
android:text="/"
android:background="@color/white"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/yi"
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="20sp"
android:layout_height="match_parent"
android:textColor="@color/black"
android:text="1"
android:background="@color/white"/>
<Button
android:id="@+id/er"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textSize="20sp"
android:textColor="@color/black"
android:text="2"
android:background="@color/white"/>
<Button
android:id="@+id/san"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textColor="@color/black"
android:textSize="20sp"
android:text="3"
android:background="@color/white"/>
<Button
android:id="@+id/sub"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textColor="@color/black"
android:textSize="20sp"
android:text="-"
android:background="@color/white"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/zero"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textColor="@color/black"
android:textSize="20sp"
android:text="0"
android:background="@color/white"/>
<Button
android:id="@+id/point"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textColor="@color/black"
android:textSize="20sp"
android:text="."
android:background="@color/white"/>
<Button
android:id="@+id/equal"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textColor="@color/black"
android:textSize="20sp"
android:text="="
android:background="@color/white"/>
<Button
android:id="@+id/add"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textColor="@color/black"
android:textSize="20sp"
android:text="+"
android:background="@color/white"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
代碼
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private String tv="0";
private ActivityMainBinding inflate;
private String count="";
private boolean isMax=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inflate = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(inflate.getRoot());
inflate.zero.setOnClickListener(this);
inflate.yi.setOnClickListener(this);
inflate.er.setOnClickListener(this);
inflate.san.setOnClickListener(this);
inflate.si.setOnClickListener(this);
inflate.wu.setOnClickListener(this);
inflate.liu.setOnClickListener(this);
inflate.qi.setOnClickListener(this);
inflate.ba.setOnClickListener(this);
inflate.jiu.setOnClickListener(this);
inflate.add.setOnClickListener(this);
inflate.sub.setOnClickListener(this);
inflate.mul.setOnClickListener(this);
inflate.div.setOnClickListener(this);
inflate.point.setOnClickListener(this);
inflate.equal.setOnClickListener(this);
inflate.dec.setOnClickListener(this);
inflate.ac.setOnClickListener(this);
}
private void jisuanqi(){
List<String> strs=new ArrayList<>();
StringBuilder f= new StringBuilder();
for(int i=0;i<tv.length();i++) {
if(String.valueOf(tv.charAt(i)).equals("-")) {
if(!f.toString().equals("")){
strs.add(f.toString());
strs.add("-");
f = new StringBuilder();
}else{
f.append("-");
}
continue;
}
if(String.valueOf(tv.charAt(i)).matches("(\\d+)|(\\.)")) {
f.append(String.valueOf(tv.charAt(i)));
}else {
strs.add(f.toString());
strs.add(String.valueOf(tv.charAt(i)));
f = new StringBuilder();
}
}
if(!f.toString().equals("")) {
strs.add(f.toString());
}
if(!strs.get(strs.size()-1).matches("(\\-|)?\\d+(\\.\\d*)?$")){
strs.remove(strs.size()-1);
}
double ans=number(strs,0);
if(ans==0||ans%(int)ans==0){
count=(int)ans+"";
}else {
BigDecimal bigdecimal=new BigDecimal(ans);
String a=bigdecimal.setScale(12,5).toString();
if(ans-(int)ans!=0) {
while (String.valueOf(a.charAt(a.length() - 1)).equals("0")){
a=a.substring(0,a.length()-1);
}
}
count=a;
}
if(isMax){
isMax=false;
count="∞";
}
showText();
}
private void showText(){
inflate.tv.setText(tv);
SpannableString spanText = new SpannableString(count);
spanText.setSpan(new AbsoluteSizeSpan(30, true), 0, spanText.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
inflate.tv.append("\n");
inflate.tv.append(spanText);
}
private double number(List<String> n,int v) {
double con = 0;
int j=0;
con=Double.valueOf(n.get(0));
if(v==0) {
for(int i=0;i<n.size();i++) {
if(n.get(i).matches("(\\-|)?\\d+(\\.\\d+)?$")) {//55+5*5/5-5=
con=Double.valueOf(n.get(i));
if(n.size()>i+1) {
j=i+1;
}
}
if(n.get(j).equals("x")){
n.set(i, String.valueOf(con*Double.valueOf(n.get(i+2))));
n.remove(i+1);
n.remove(i+1);
return number(n,0);
}else if(n.get(j).equals("/")){
if(Double.valueOf(n.get(i+2))==0){
isMax=true;
return 10;
}
n.set(i, String.valueOf(con/Double.valueOf(n.get(i+2))));
n.remove(i+1);
n.remove(i+1);
return number(n,0);
}
}
}
for(int i=0;i<n.size();i++) {
if(n.get(i).matches("(\\-|)?\\d+(\\.\\d+)?$")) {//55+5*5/5-5=
con=Double.valueOf(n.get(i));
if(n.size()>i+1) {
j=i+1;
}
}
if(n.get(j).equals("+")){
n.set(i, String.valueOf(con+Double.valueOf(n.get(i+2))));
n.remove(i+1);
n.remove(i+1);
return number(n,1);
}else if(n.get(j).equals("-")){
n.set(i, String.valueOf(con-Double.valueOf(n.get(i+2))));
n.remove(i+1);
n.remove(i+1);
return number(n,1);
}
}
return con;
}
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.point:
if(String.valueOf(tv.charAt(tv.length()-1)).matches("\\d+")){
tv+=".";
inflate.tv.setText(tv);
}
break;
case R.id.zero:
case R.id.yi:
case R.id.er:
case R.id.san:
case R.id.si:
case R.id.wu:
case R.id.liu:
case R.id.qi:
case R.id.ba:
case R.id.jiu:
if(tv.equals("0")){
tv="";
}
Button bt_digit=findViewById(v.getId());
tv+=bt_digit.getText();
inflate.tv.setText(tv);
count=tv;
jisuanqi();
break;
case R.id.sub:
if(String.valueOf(tv.charAt(0)).equals("0")){
tv="-";
}else {
if(String.valueOf(tv.charAt(tv.length()-1)).equals(".")){
tv += "0";
}
tv+="-";
}
showText();
break;
case R.id.add:
case R.id.mul:
case R.id.div:
Button bt_operator=findViewById(v.getId());
String laststr=String.valueOf(tv.charAt(tv.length()-1));
if(!laststr.matches("(\\d+)") ) {
tv = tv.substring(0, tv.length() - 1) + bt_operator.getText();
}else {
if(laststr.equals(".")) {
tv += "0";
}
tv+=bt_operator.getText();
}
showText();
break;
case R.id.ac:
tv="0";
inflate.tv.setText(tv);
break;
case R.id.dec:
if(tv.length()>1) {
tv = tv.substring(0, tv.length() - 1);
}else {
tv="0";
}
jisuanqi();
break;
case R.id.equal:
if(!String.valueOf(tv.charAt(tv.length()-1)).matches("(\\d+)")){
tv = tv.substring(0, tv.length() - 1);
}
count=tv;
jisuanqi();
tv=count;
inflate.tv.setText(tv);
break;
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開發(fā)實(shí)現(xiàn)的簡(jiǎn)單計(jì)算器功能【附完整demo源碼下載】
- android計(jì)算器簡(jiǎn)單實(shí)現(xiàn)代碼
- 簡(jiǎn)單實(shí)現(xiàn)Android計(jì)算器功能
- Android實(shí)戰(zhàn)教程第一篇之最簡(jiǎn)單的計(jì)算器
- Android計(jì)算器簡(jiǎn)單邏輯實(shí)現(xiàn)實(shí)例分享
- Android開發(fā)學(xué)習(xí)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- Android開發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- Android實(shí)現(xiàn)簡(jiǎn)單加法計(jì)算器
- Android簡(jiǎn)單實(shí)現(xiàn)計(jì)算器功能
- Android實(shí)戰(zhàn)項(xiàng)目之實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器
相關(guān)文章
android 點(diǎn)擊EditText始終不彈出軟件鍵盤實(shí)現(xiàn)代碼
這篇文章主要介紹了android 點(diǎn)擊EditText始終不彈出軟件鍵盤實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11
Android 詳解沉浸式狀態(tài)欄的實(shí)現(xiàn)流程
沉浸式就是要給用戶提供完全沉浸的體驗(yàn),使用戶有一種置身于虛擬世界之中的感覺。沉浸式模式就是整個(gè)屏幕中顯示都是應(yīng)用的內(nèi)容,沒有狀態(tài)欄也沒有導(dǎo)航欄,用戶不會(huì)被一些系統(tǒng)的界面元素所打擾,讓我們來實(shí)現(xiàn)下網(wǎng)上傳的沸沸揚(yáng)揚(yáng)的安卓沉浸式狀態(tài)欄2021-11-11
Android布局之LinearLayout自定義高亮背景的方法
這篇文章主要介紹了Android布局之LinearLayout自定義高亮背景的方法,實(shí)例分析了Android中LinearLayout布局參數(shù)設(shè)置技巧,需要的朋友可以參考下2016-01-01
Android SQLite操作之大數(shù)據(jù)處理與同時(shí)讀寫方法
這篇文章主要介紹了Android SQLite操作之大數(shù)據(jù)處理與同時(shí)讀寫方法,實(shí)例分析了Android操作SQLite時(shí)基于事務(wù)的數(shù)據(jù)緩存與批量插入技巧,以及同時(shí)讀寫的相關(guān)實(shí)現(xiàn)方法與注意事項(xiàng),需要的朋友可以參考下2016-07-07
android閱讀器長(zhǎng)按選擇文字功能實(shí)現(xiàn)代碼
本篇文章主要介紹了android閱讀器長(zhǎng)按選擇文字功能實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
Android沉浸式狀態(tài)欄實(shí)現(xiàn)
這篇文章主要介紹了Android沉浸式狀態(tài)欄實(shí)現(xiàn),即一體化狀態(tài)欄實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-01-01

