Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器
如果是制作簡(jiǎn)易計(jì)算器的話是基本沒(méi)有難點(diǎn)的,供大家參考,具體內(nèi)容如下
步驟是先寫(xiě)好界面布局,將按鈕的布局、字號(hào)顏色啥的做好,再就是設(shè)置監(jiān)聽(tīng)器。
使用了NoTitleBar的主題
代碼如下:
activity_main里關(guān)于界面的代碼:
<?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="fill_parent"
android:layout_height="90dp"
android:background="@drawable/white_bg"
android:editable="false"
android:gravity="right|bottom"
android:paddingBottom="20dp"
android:paddingRight="20dp"
android:textSize="50sp" />
<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/bt_clr"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="C"
android:gravity="right|bottom"
android:textSize="30sp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/>
<Button
android:id="@+id/bt_del"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="DEL"
android:gravity="right|bottom"
android:textSize="30sp"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/>
<Button
android:id="@+id/bt_div"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="÷"
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/>
<Button
android:id="@+id/bt_mul"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="×"
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
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/bt_7"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="7"
android:gravity="right|bottom"
android:textSize="30sp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/>
<Button
android:id="@+id/bt_8"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="8"
android:gravity="right|bottom"
android:textSize="30sp"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/>
<Button
android:id="@+id/bt_9"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="9"
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/>
<Button
android:id="@+id/bt_sub"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="-"
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
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/bt_4"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="4"
android:gravity="right|bottom"
android:textSize="30sp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/>
<Button
android:id="@+id/bt_5"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="5"
android:gravity="right|bottom"
android:textSize="30sp"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/>
<Button
android:id="@+id/bt_6"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="6"
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/>
<Button
android:id="@+id/bt_add"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="+"
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
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/bt_1"
android:text="1"
android:textSize="30sp"
android:gravity="right|bottom"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/>
<Button
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/bt_2"
android:text="2"
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/>
<Button
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/bt_3"
android:text="3"
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
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/bt_0"
android:text="0"
android:textSize="30sp"
android:gravity="right|bottom"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/>
<Button
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/bt_pt"
android:text="."
android:textSize="30sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:background="@drawable/white_selector"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/>
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/bt_eq"
android:layout_width="80dp"
android:layout_height="170dp"
android:layout_marginLeft="10dp"
android:background="@drawable/orange_selector"
android:gravity="right|bottom"
android:text="="
android:textSize="30sp"
android:paddingRight="15sp"
android:paddingBottom="15sp"
/>
</LinearLayout>
</LinearLayout>
Mainactivity的代碼:
package com.example.administrator.calculatordemo;
import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements View.OnClickListener{
Button bt_0,bt_1,bt_2,bt_3,bt_4,bt_5,bt_6,bt_7,bt_8,bt_9,bt_pt;
Button bt_mul,bt_div,bt_add,bt_sub;
Button bt_clr,bt_del,bt_eq;
EditText et_input;
boolean clr_flag; //判斷et中是否清空
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//實(shí)例化對(duì)象
setContentView(R.layout.activity_main);
bt_0= (Button) findViewById(R.id.bt_0);
bt_1= (Button) findViewById(R.id.bt_1);
bt_2= (Button) findViewById(R.id.bt_2);
bt_3= (Button) findViewById(R.id.bt_3);
bt_4= (Button) findViewById(R.id.bt_4);
bt_5= (Button) findViewById(R.id.bt_5);
bt_6= (Button) findViewById(R.id.bt_6);
bt_7= (Button) findViewById(R.id.bt_7);
bt_8= (Button) findViewById(R.id.bt_8);
bt_9= (Button) findViewById(R.id.bt_9);
bt_pt= (Button) findViewById(R.id.bt_pt);
bt_add= (Button) findViewById(R.id.bt_add);
bt_sub= (Button) findViewById(R.id.bt_sub);
bt_mul= (Button) findViewById(R.id.bt_mul);
bt_div= (Button) findViewById(R.id.bt_div);
bt_clr= (Button) findViewById(R.id.bt_clr);
bt_del= (Button) findViewById(R.id.bt_del);
bt_eq= (Button) findViewById(R.id.bt_eq);
et_input= (EditText) findViewById(R.id.et_input);
//設(shè)置按鈕的點(diǎn)擊事件
bt_0.setOnClickListener(this);
bt_1.setOnClickListener(this);
bt_2.setOnClickListener(this);
bt_3.setOnClickListener(this);
bt_4.setOnClickListener(this);
bt_5.setOnClickListener(this);
bt_6.setOnClickListener(this);
bt_7.setOnClickListener(this);
bt_8.setOnClickListener(this);
bt_9.setOnClickListener(this);
bt_pt.setOnClickListener(this);
bt_add.setOnClickListener(this);
bt_sub.setOnClickListener(this);
bt_mul.setOnClickListener(this);
bt_div.setOnClickListener(this);
bt_clr.setOnClickListener(this);
bt_del.setOnClickListener(this);
bt_eq.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String str=et_input.getText().toString();
switch (v.getId()){
case R.id.bt_0:
case R.id.bt_1:
case R.id.bt_2:
case R.id.bt_3:
case R.id.bt_4:
case R.id.bt_5:
case R.id.bt_6:
case R.id.bt_7:
case R.id.bt_8:
case R.id.bt_9:
case R.id.bt_pt:
if(clr_flag){
clr_flag=false;
str="";
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.bt_add:
case R.id.bt_sub:
case R.id.bt_mul:
case R.id.bt_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.bt_clr:
if(clr_flag)
clr_flag=false;
str="";
et_input.setText("");
break;
case R.id.bt_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.bt_eq: //單獨(dú)運(yùn)算最后結(jié)果
getResult();
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為空
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不是空
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("");
}
}
}
關(guān)于計(jì)算器的精彩文章請(qǐng)查看《計(jì)算器專題》 ,更多精彩等你來(lái)發(fā)現(xiàn)!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 從零開(kāi)始學(xué)android實(shí)現(xiàn)計(jì)算器功能示例分享(計(jì)算器源碼)
- Android開(kāi)發(fā)實(shí)現(xiàn)的簡(jiǎn)單計(jì)算器功能【附完整demo源碼下載】
- android計(jì)算器簡(jiǎn)單實(shí)現(xiàn)代碼
- Android計(jì)算器編寫(xiě)代碼
- android計(jì)時(shí)器,時(shí)間計(jì)算器的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)簡(jiǎn)易計(jì)算器小程序
- android studio實(shí)現(xiàn)計(jì)算器
- Android中使用GridLayout網(wǎng)格布局來(lái)制作簡(jiǎn)單的計(jì)算器App
- Android studio設(shè)計(jì)簡(jiǎn)易計(jì)算器
- Android簡(jiǎn)單實(shí)現(xiàn)計(jì)算器功能
相關(guān)文章
Flutter使用AnimatedOpacity實(shí)現(xiàn)圖片漸現(xiàn)動(dòng)畫(huà)
其實(shí)在Flutter中提供了一些封裝好的動(dòng)畫(huà)組件,以便我們快速應(yīng)用。本文將利用其中的AnimatedOpacity組件實(shí)現(xiàn)圖片漸現(xiàn)動(dòng)畫(huà)效果,需要的可以參考一下2022-03-03
Android相冊(cè)效果(使用C#和Java分別實(shí)現(xiàn))
這篇文章主要介紹了Android相冊(cè)效果(使用C#和Java分別實(shí)現(xiàn)),原來(lái)C#也可以開(kāi)發(fā)APP,小編第一次見(jiàn)了~感覺(jué)不錯(cuò),因?yàn)樾【帟簳r(shí)不喜歡Java,所以,需要的朋友可以參考下2015-06-06
Android實(shí)現(xiàn)帶附件的郵件發(fā)送功能
這篇文章主要介紹了Android實(shí)現(xiàn)帶附件的郵件發(fā)送功能的相關(guān)資料,android發(fā)送郵件有兩種方式,本文重點(diǎn)介紹基于JMail實(shí)現(xiàn)郵件發(fā)送功能,感興趣的小伙伴們可以參考一下2016-01-01
android通過(guò)藍(lán)牙接收文件打開(kāi)時(shí)無(wú)法自動(dòng)選擇合適的應(yīng)用程序
android 通過(guò)藍(lán)牙接收文件,從歷史傳輸記錄打開(kāi),無(wú)法自動(dòng)選擇合適的應(yīng)用程序,比如video player打開(kāi).3gp、.mp4文件等等2013-06-06
Android實(shí)現(xiàn)EventBus登錄界面與傳值(粘性事件)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)EventBus登錄界面與傳值,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android實(shí)現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單(1)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單第一篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
android真機(jī)調(diào)試時(shí)無(wú)法顯示logcat信息的解決方法介紹
以下是對(duì)android真機(jī)調(diào)試時(shí)無(wú)法顯示logcat信息的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-07-07
Android中TextView顯示圓圈背景或設(shè)置圓角的方法
TextView顯示文本給用戶,并允許他們選擇編輯。TextView是一個(gè)完整的文本編輯器,但是其基本類配置為不允許編輯。下面這篇文章主要給大家介紹了關(guān)于Android中TextView顯示圓圈背景或設(shè)置圓角的方法,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-05-05
Flutter中數(shù)據(jù)庫(kù)的使用教程詳解
在Flutter開(kāi)發(fā)過(guò)程中,有時(shí)需要對(duì)數(shù)據(jù)進(jìn)行本地的持久化存儲(chǔ),使用sp文件形式雖然也能解決問(wèn)題,但是有時(shí)數(shù)據(jù)量較大的時(shí)候,顯然我們文件形式就不太合適了,這時(shí)候我們就需要使用數(shù)據(jù)庫(kù)進(jìn)行存儲(chǔ)。本文將詳細(xì)講講Flutter中數(shù)據(jù)庫(kù)的使用,需要的可以參考一下2022-04-04

