基于Android實現(xiàn)計算器計算功能
一.新建一個項目
步驟:
1.新建項目

2.選擇


二.用戶界面構(gòu)建
找到項目的res的下面layout里面的activity.xml文件進(jìn)行約束布局界面構(gòu)建。
activity.xml代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="MissingConstraints">
<EditText
android:id="@+id/ed_input"
android:layout_width="match_parent"
android:layout_height="100dp"
android:hint="輸入框" />
<EditText
android:id="@+id/ed_output"
android:layout_width="match_parent"
android:layout_height="100dp"
android:hint="輸出口" />
</GridLayout>
<GridLayout
android:layout_width="424dp"
android:layout_height="329dp"
android:columnCount="4"
android:rowCount="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/gridLayout"
tools:ignore="MissingConstraints">
<Button
android:id="@+id/buttonc"
android:layout_width="180dp"
android:layout_height="60dp"
android:layout_columnSpan="2"
android:backgroundTint="#a6a6a6"
android:text="c" />
<Button
android:id="@+id/buttondel"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_columnSpan="1"
android:backgroundTint="#a6a6a6"
android:text="DEL" />
<Button
android:id="@+id/buttonchu"
android:layout_width="90dp"
android:layout_height="60dp"
android:backgroundTint="#ff9500"
android:text="/" />
<Button
android:id="@+id/button7"
android:layout_width="90dp"
android:layout_height="60dp"
android:backgroundTint="#333333"
android:text="7" />
<Button
android:id="@+id/button8"
android:layout_width="90dp"
android:layout_height="60dp"
android:backgroundTint="#333333"
android:text="8" />
<Button
android:id="@+id/button9"
android:layout_width="90dp"
android:layout_height="60dp"
android:backgroundTint="#333333"
android:text="9" />
<Button
android:id="@+id/buttoncheng"
android:layout_width="90dp"
android:layout_height="60dp"
android:backgroundTint="#ff9500"
android:text="*" />
<Button
android:id="@+id/button4"
android:layout_width="90dp"
android:layout_height="60dp"
android:backgroundTint="#333333"
android:text="4" />
<Button
android:id="@+id/button5"
android:layout_width="90dp"
android:layout_height="60dp"
android:backgroundTint="#333333"
android:text="5" />
<Button
android:id="@+id/button6"
android:layout_width="90dp"
android:layout_height="60dp"
android:backgroundTint="#333333"
android:text="6" />
<Button
android:id="@+id/buttonjian"
android:layout_width="90dp"
android:layout_height="60dp"
android:backgroundTint="#ff9500"
android:text="-" />
<Button
android:id="@+id/button1"
android:layout_width="90dp"
android:layout_height="60dp"
android:backgroundTint="#333333"
android:text="1" />
<Button
android:id="@+id/button2"
android:layout_width="90dp"
android:layout_height="60dp"
android:backgroundTint="#333333"
android:text="2" />
<Button
android:id="@+id/button3"
android:layout_width="90dp"
android:layout_height="60dp"
android:backgroundTint="#333333"
android:text="3" />
<Button
android:id="@+id/buttonjia"
android:layout_width="90dp"
android:layout_height="60dp"
android:backgroundTint="#ff9500"
android:text="+" />
<Button
android:id="@+id/buttonyuliu"
android:layout_width="90dp"
android:layout_height="60dp"
android:backgroundTint="#333333"
android:text="預(yù)留" />
<Button
android:id="@+id/button0"
android:layout_width="90dp"
android:layout_height="60dp"
android:backgroundTint="#333333"
android:text="0" />
<Button
android:id="@+id/buttondian"
android:layout_width="90dp"
android:layout_height="60dp"
android:backgroundTint="#333333"
android:text="." />
<Button
android:id="@+id/buttondeng"
android:layout_width="90dp"
android:layout_height="60dp"
android:backgroundTint="#ff9500"
android:text="=" />
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>三.設(shè)置實現(xiàn)計算功能的關(guān)鍵
找到Java里面的MainActiviy.java寫入實現(xiàn)代碼。
MainActiviy.java代碼如下:
package com.example.myapplication2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button mbutton1,mbutton2,mbutton3,mbutton4,mbutton5,mbutton6,mbutton7,mbutton8,mbutton9,mbutton0,
mbuttonc,mbuttondel,mbuttonyuliu,mbuttonjia,mbuttonjian,
mbuttoncheng,mbuttonchu,mbuttondian,mbuttondeng;
private EditText edinput,edoutput;
private boolean deng_flag=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//數(shù)字0-9
mbutton1=findViewById(R.id.button1);
mbutton2=findViewById(R.id.button2);
mbutton3=findViewById(R.id.button3);
mbutton4=findViewById(R.id.button4);
mbutton5=findViewById(R.id.button5);
mbutton6=findViewById(R.id.button6);
mbutton7=findViewById(R.id.button7);
mbutton8=findViewById(R.id.button8);
mbutton9=findViewById(R.id.button9);
mbutton0=findViewById(R.id.button0);
//c、del、預(yù)留
mbuttonc=findViewById(R.id.buttonc);
mbuttondel=findViewById(R.id.buttondel);
mbuttonyuliu=findViewById(R.id.buttonyuliu);
//加減乘除、點、等號
mbuttonjia=findViewById(R.id.buttonjia);
mbuttonjian=findViewById(R.id.buttonjian);
mbuttoncheng=findViewById(R.id.buttoncheng);
mbuttonchu=findViewById(R.id.buttonchu);
mbuttondeng=findViewById(R.id.buttondeng);
mbuttondian=findViewById(R.id.buttondian);
//輸入輸出
edinput=findViewById(R.id.ed_input);
edoutput=findViewById(R.id.ed_output);
//設(shè)置按鈕監(jiān)聽
//0-9
mbutton0.setOnClickListener(this);
mbutton1.setOnClickListener(this);
mbutton2.setOnClickListener(this);
mbutton3.setOnClickListener(this);
mbutton4.setOnClickListener(this);
mbutton5.setOnClickListener(this);
mbutton6.setOnClickListener(this);
mbutton7.setOnClickListener(this);
mbutton8.setOnClickListener(this);
mbutton9.setOnClickListener(this);
//c、del、預(yù)留
mbuttonc.setOnClickListener(this);
mbuttondel.setOnClickListener(this);
mbuttonyuliu.setOnClickListener(this);
//加減乘除、點、等號
mbuttonjia.setOnClickListener(this);
mbuttonjian.setOnClickListener(this);
mbuttoncheng.setOnClickListener(this);
mbuttonchu.setOnClickListener(this);
mbuttondeng.setOnClickListener(this);
mbuttondian.setOnClickListener(this);
}
@Override
public void onClick(View view)
{
String input = edinput.getText().toString();
String output = edoutput.getText().toString();
switch (view.getId()){
//0-9
case R.id.button0:
case R.id.button1:
case R.id.button2:
case R.id.button3:
case R.id.button4:
case R.id.button5:
case R.id.button6:
case R.id.button7:
case R.id.button8:
case R.id.button9:
case R.id.buttondian:
if(deng_flag){
deng_flag=false;
edinput.setText(null);
edinput.setText(((Button) view).getText());
}else {
edinput.setText(input+((Button) view).getText());
}
edinput.setText(input+((Button) view).getText());
break;
//c
case R.id.buttonc:
edinput.setText(null);
edoutput.setText(null);
break;
//del
case R.id.buttondel:
if (deng_flag){
deng_flag=false;
edinput.setText("");
}else if(input !=null&&!input.equals("")){
edinput.setText(input.substring(0,input.length()-1));
}
break;
//預(yù)留
case R.id.buttonyuliu:
break;
//加減乘除
case R.id.buttonjia:
case R.id.buttonjian:
case R.id.buttoncheng:
case R.id.buttonchu:
edinput.setText(input+" "+((Button) view).getText()+" ");
break;
//等號
case R.id.buttondeng:
// edinput.setText(input+((Button) view).getText());
// break;
getResult();
}
}
private void getResult() {
try{
String input = edinput.getText().toString();
int iResult=0;
double dResult=0;
String cw="錯誤";
String s1,s2,op;//數(shù)字,數(shù)字,操作符 s1"4" op"*" s2"5"
s1=input.substring(0,input.indexOf(" "));
op=input.substring(input.indexOf(" ")+1,input.indexOf(" ")+2);
s2=input.substring(input.indexOf(" ")+3);
double d1,d2;
d1=Double.parseDouble(s1);
d2=Double.parseDouble(s2);
if(op.equals("+")){//加
dResult=d1+d2;
// edoutput.setText(dResult+"");
}else if(op.equals("-")){//減
dResult=d1-d2;
} else if (op.equals("*")){//乘
dResult=d1*d2;
} else if (op.equals("/")) {//除
if(d2==0){
edoutput.setText(cw+"");
} else if (d1==0) {
dResult=0;
} else {
dResult=d1/d2;
}
}
if(!input.equals(".")&&!input.equals("/")){
iResult=(int)dResult;
edoutput.setText(iResult+"");
}
edoutput.setText(dResult+"");
}catch (Exception e){
System.out.println(e);
}
}
}運行結(jié)果如下:

輸入計算值,得出結(jié)果

以上就是基于Android實現(xiàn)計算機(jī)計算功能的詳細(xì)內(nèi)容,更多關(guān)于Android計算功能的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
第1個Android應(yīng)用程序 Android制作簡單單頁導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了第1個Android應(yīng)用程序PhonewordApp:Android制作簡單單頁導(dǎo)航,感興趣的小伙伴們可以參考一下2016-06-06
Android實現(xiàn)TextView中的部分文字實現(xiàn)點擊跳轉(zhuǎn)功能
在移動端應(yīng)用中,往往需要在一段文字中讓某幾個關(guān)鍵詞或短語具備點擊跳轉(zhuǎn)功能,如果將整段文字放到 Button、Link 或單獨的 View 中,通常會帶來布局復(fù)雜、樣式難以統(tǒng)一、可維護(hù)性差等問題,所以本文將給大家介紹Android實現(xiàn)TextView中的部分文字實現(xiàn)點擊跳轉(zhuǎn)功能2025-04-04
Android ViewPager循環(huán)播放廣告實例詳解
這篇文章主要介紹了Android ViewPager循環(huán)播放廣告條實例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
android實現(xiàn)okHttp的get和post請求的簡單封裝與使用
這篇文章主要介紹了android實現(xiàn)okHttp的get和post請求的簡單封裝與使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
OpenGL Shader實現(xiàn)光照發(fā)光體特效
這篇文章主要介紹了如何通過OpenGL Shader實現(xiàn)光照發(fā)光體特效,不同于陰影遮蓋,它是利用圓形繪制向內(nèi)部。感興趣的小伙伴可以了解一下2022-02-02
TabLayout關(guān)聯(lián)ViewPager后不顯示文字的解決方法
這篇文章主要為大家詳細(xì)介紹了TabLayout關(guān)聯(lián)ViewPager后不顯示文字的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
Android用RecyclerView實現(xiàn)圖標(biāo)拖拽排序以及增刪管理
這篇文章主要介紹了Android用RecyclerView實現(xiàn)圖標(biāo)拖拽排序以及增刪管理的方法,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03
解決Android-RecyclerView列表倒計時錯亂問題
這篇文章主要介紹了解決Android-RecyclerView列表倒計時錯亂問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08

