Android開(kāi)發(fā)學(xué)習(xí)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
這里是用線性布局實(shí)現(xiàn)的計(jì)算器,為防止以后再回顧知識(shí)代碼找不到,特將代碼貼在這里:
xml文件的布局代碼:
<?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" tools:context=".MainActivity"> <EditText android:id="@+id/et_input" android:layout_width="match_parent" android:layout_height="100dp" android:paddingBottom="5dp" android:paddingRight="5dp" android:textSize="50sp" android:gravity="right" android:textColor="#00ff00"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:orientation="horizontal" android:gravity="center_horizontal"> <Button android:id="@+id/btn_clr" android:layout_width="80dp" android:layout_height="80dp" android:text="C" android:textSize="30sp" android:paddingRight="15sp" android:paddingBottom="15sp" android:textColor="#ff0000"/> <Button android:id="@+id/btn_del" android:layout_width="80dp" android:layout_height="80dp" android:text="Ba" android:textSize="30sp" android:layout_marginLeft="10dp" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android:id="@+id/btn_add" android:layout_width="80dp" android:layout_height="80dp" android:text="+" android:textSize="30sp" android:layout_marginLeft="10dp" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android:id="@+id/btn_sub" android:layout_width="80dp" android:layout_height="80dp" android:text="-" android:textSize="30sp" android:layout_marginLeft="10dp" android:paddingRight="15sp" android:paddingBottom="15sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal" android:gravity="center_horizontal" > <Button android:id="@+id/btn_7" android:layout_width="80dp" android:layout_height="80dp" android:text="7" android:textSize="30sp" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android:id="@+id/btn_8" android:layout_width="80dp" android:layout_height="80dp" android:text="8" android:textSize="30sp" android:layout_marginLeft="10dp" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android:id="@+id/btn_9" android:layout_width="80dp" android:layout_height="80dp" android:text="9" android:textSize="30sp" android:layout_marginLeft="10dp" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android:id="@+id/btn_mul" android:layout_width="80dp" android:layout_height="80dp" android:text="×" android:textSize="30sp" android:layout_marginLeft="10dp" android:paddingRight="15sp" android:paddingBottom="15sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal" android:gravity="center_horizontal" > <Button android:id="@+id/btn_4" android:layout_width="80dp" android:layout_height="80dp" android:text="4" android:textSize="30sp" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android:id="@+id/btn_5" android:layout_width="80dp" android:layout_height="80dp" android:text="5" android:textSize="30sp" android:layout_marginLeft="10dp" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android:id="@+id/btn_6" android:layout_width="80dp" android:layout_height="80dp" android:text="6" android:textSize="30sp" android:layout_marginLeft="10dp" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android:id="@+id/btn_div" android:layout_width="80dp" android:layout_height="80dp" android:text="÷" android:textSize="30sp" android:layout_marginLeft="10dp" android:paddingRight="15sp" android:paddingBottom="15sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp" android:gravity="center_horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="80dp" android:layout_height="80dp" android:id="@+id/btn_1" android:text="1" android:textSize="30sp" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android:layout_width="80dp" android:layout_height="80dp" android:id="@+id/btn_2" android:text="2" android:textSize="30sp" android:layout_marginLeft="10dp" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android:layout_width="80dp" android:layout_height="80dp" android:id="@+id/btn_3" android:text="3" android:textSize="30sp" android:layout_marginLeft="10dp" android:paddingRight="15sp" android:paddingBottom="15sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp"> <Button android:layout_width="170dp" android:layout_height="80dp" android:id="@+id/btn_0" android:text="0" android:textSize="30sp" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android:layout_width="80dp" android:layout_height="80dp" android:id="@+id/btn_pt" android:text="." android:textSize="30sp" android:layout_marginLeft="10dp" android:paddingRight="15sp" android:paddingBottom="15sp" /> </LinearLayout> </LinearLayout> <Button android:id="@+id/btn_eq" android:layout_width="80dp" android:layout_height="170dp" android:layout_marginLeft="10dp" android:text="=" android:textSize="30sp" android:paddingRight="15sp" android:paddingBottom="15sp" android:gravity="center" android:background="@color/colorAccent"/> </LinearLayout> </LinearLayout>
Activity.java文件實(shí)現(xiàn)功能的代碼如下:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Main2Activity extends AppCompatActivity implements View.OnClickListener{
//創(chuàng)建Button對(duì)象 也就是activity_main.xml里所設(shè)置的ID
Button btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_pt;
Button btn_mul,btn_div,btn_add,btn_sub;
Button btn_clr,btn_del,btn_eq;
EditText et_input;
boolean clr_flag; //判斷et編輯文本框中是否清空
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//實(shí)例化對(duì)象
setContentView(R.layout.activity_main2);
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_pt= (Button) findViewById(R.id.btn_pt);
btn_add= (Button) findViewById(R.id.btn_add);
btn_sub= (Button) findViewById(R.id.btn_sub);
btn_mul= (Button) findViewById(R.id.btn_mul);
btn_div= (Button) findViewById(R.id.btn_div);
btn_clr= (Button) findViewById(R.id.btn_clr);
btn_del= (Button) findViewById(R.id.btn_del);
btn_eq= (Button) findViewById(R.id.btn_eq);
et_input= (EditText) findViewById(R.id.et_input);
//給按鈕設(shè)置的點(diǎn)擊事件
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_pt.setOnClickListener(this);
btn_add.setOnClickListener(this);
btn_sub.setOnClickListener(this);
btn_mul.setOnClickListener(this);
btn_div.setOnClickListener(this);
btn_clr.setOnClickListener(this);
btn_del.setOnClickListener(this);
btn_eq.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_pt:
if(clr_flag){
clr_flag=false;
str="";
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.btn_add:
case R.id.btn_sub:
case R.id.btn_mul:
case R.id.btn_div:
if(clr_flag){
clr_flag=false;
str="";
et_input.setText("");
}
if(str.contains("+")||str.contains("-")||str.contains("×")||str.contains("÷")) {
str=str.substring(0,str.indexOf(" "));
}
et_input.setText(str+" "+((Button)v).getText()+" ");
break;
case R.id.btn_clr:
if(clr_flag)
clr_flag=false;
str="";
et_input.setText("");
break;
case R.id.btn_del: //判斷是否為空,然后在進(jìn)行刪除
if(clr_flag){
clr_flag=false;
str="";
et_input.setText("");
}
else if(str!=null&&!str.equals("")){
et_input.setText(str.substring(0,str.length()-1));
}
break;
case R.id.btn_eq: //單獨(dú)運(yùn)算最后結(jié)果
getResult();//調(diào)用下面的方法
break;
}
}
private void getResult() {
String exp=et_input.getText().toString();
if(exp==null||exp.equals("")) return ;
//因?yàn)闆](méi)有運(yùn)算符所以不用運(yùn)算
if(!exp.contains(" ")){
return ;
}
if(clr_flag){
clr_flag=false;
return;
}
clr_flag=true;
//截取運(yùn)算符前面的字符串
String s1=exp.substring(0,exp.indexOf(" "));
//截取的運(yùn)算符
String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);
//截取運(yùn)算符后面的字符串
String s2=exp.substring(exp.indexOf(" ")+3);
double cnt=0;
if(!s1.equals("")&&!s2.equals("")){
double d1=Double.parseDouble(s1);
double d2=Double.parseDouble(s2);
if(op.equals("+")){
cnt=d1+d2;
}
if(op.equals("-")){
cnt=d1-d2;
}
if(op.equals("×")){
cnt=d1*d2;
}
if(op.equals("÷")){
if(d2==0) cnt=0;
else cnt=d1/d2;
}
if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")) {
int res = (int) cnt;
et_input.setText(res+"");
}else {
et_input.setText(cnt+"");}
}
//如果s1是空 s2不是空 就執(zhí)行下一步
else if(!s1.equals("")&&s2.equals("")){
double d1=Double.parseDouble(s1);
if(op.equals("+")){
cnt=d1;
}
if(op.equals("-")){
cnt=d1;
}
if(op.equals("×")){
cnt=0;
}
if(op.equals("÷")){
cnt=0;
}
if(!s1.contains(".")) {
int res = (int) cnt;
et_input.setText(res+"");
}else {
et_input.setText(cnt+"");}
}
//如果s1是空 s2不是空 就執(zhí)行下一步
else if(s1.equals("")&&!s2.equals("")){
double d2=Double.parseDouble(s2);
if(op.equals("+")){
cnt=d2;
}
if(op.equals("-")){
cnt=0-d2;
}
if(op.equals("×")){
cnt=0;
}
if(op.equals("÷")){
cnt=0;
}
if(!s2.contains(".")) {
int res = (int) cnt;
et_input.setText(res+"");
}else {
et_input.setText(cnt+"");}
}
else {
et_input.setText("");
}
}
}
結(jié)果圖如下所示:

更多計(jì)算器功能實(shí)現(xiàn),請(qǐng)點(diǎn)擊專題: 計(jì)算器功能匯總 進(jìn)行學(xué)習(xí)
關(guān)于Android計(jì)算器功能的實(shí)現(xiàn),查看專題:Android計(jì)算器 進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開(kāi)發(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開(kāi)發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- Android實(shí)現(xiàn)簡(jiǎn)單加法計(jì)算器
- Android簡(jiǎn)單實(shí)現(xiàn)計(jì)算器功能
- Android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- Android實(shí)戰(zhàn)項(xiàng)目之實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器
相關(guān)文章
Android使用Scroller實(shí)現(xiàn)彈性滑動(dòng)效果
這篇文章主要介紹了Android使用Scroller實(shí)現(xiàn)彈性滑動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android6.0動(dòng)態(tài)申請(qǐng)權(quán)限所遇到的問(wèn)題小結(jié)
這篇文章給大家介紹了Android6.0動(dòng)態(tài)申請(qǐng)權(quán)限所遇到的問(wèn)題,在沒(méi)給大家介紹這下問(wèn)題之前,先給大家說(shuō)下基本定義和基本使用方式,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,對(duì)android 6.0 動(dòng)態(tài)權(quán)限遇到問(wèn)題感興趣的朋友一起看看吧2016-11-11
Android控件Spinner實(shí)現(xiàn)下拉列表及監(jiān)聽(tīng)功能
這篇文章主要介紹了Android控件Spinner實(shí)現(xiàn)下拉列表及監(jiān)聽(tīng)功能,這是在Web開(kāi)發(fā)中一個(gè)必不可少的交互性組件,而在Android中的對(duì)應(yīng)實(shí)現(xiàn)就是Spinner。需要的朋友可以參考下2018-07-07
Android中TabLayout結(jié)合ViewPager實(shí)現(xiàn)頁(yè)面切換效果
這篇文章主要介紹了Android中TabLayout結(jié)合ViewPager實(shí)現(xiàn)頁(yè)面切換效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android自定義商品購(gòu)買數(shù)量加減控件
這篇文章主要為大家詳細(xì)介紹了Android自定義商品購(gòu)買數(shù)量加減控件的相關(guān)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
android ListView結(jié)合x(chóng)utils3仿微信實(shí)現(xiàn)下拉加載更多
本篇文章主要介紹了android ListView結(jié)合x(chóng)utils3仿微信實(shí)現(xiàn)下拉加載更多,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Flutter實(shí)現(xiàn)webview與原生組件組合滑動(dòng)的示例代碼
這篇文章主要介紹了Flutter實(shí)現(xiàn)webview與原生組件組合滑動(dòng)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

