android?studio?項(xiàng)目?:UI設(shè)計(jì)高精度實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
UI設(shè)計(jì):
實(shí)驗(yàn)?zāi)康模?/strong>
自主完成一個(gè)簡(jiǎn)單APP的設(shè)計(jì)工作,綜合應(yīng)用已經(jīng)學(xué)到的Android UI
設(shè)計(jì)技巧,重點(diǎn)注意合理使用布局。
實(shí)驗(yàn)要求:
- 1.完成一個(gè)計(jì)算器的設(shè)計(jì),可以以手機(jī)自帶的計(jì)算器為參考。設(shè)計(jì)過(guò)程中,注意考慮界面的美觀性,不同機(jī)型的適應(yīng)性,以及功能的完備性。
- 2.注意結(jié)合Activity的生命周期,考慮不同情況下計(jì)算器的界面狀態(tài)。
- 3.如有余力,可以考慮實(shí)現(xiàn)一個(gè)高精度科學(xué)計(jì)算型的計(jì)算器。
實(shí)現(xiàn)效果:
整數(shù)和浮點(diǎn)數(shù)的加減乘除、取余、開(kāi)根號(hào),異號(hào)、清零 計(jì)算結(jié)果全部實(shí)現(xiàn)。
精度保留到小數(shù)點(diǎn)后100位,比如下面是√3的計(jì)算結(jié)果。(可以自行修改精度)
完全滿(mǎn)足日常使用,可以取代手機(jī)自帶的計(jì)算器軟件。。。
程序主要包含Mainactivity.java
、Calculator.java
和 activity_main.xml
三個(gè)文件,分別用于控制、邏輯和視圖。符合MVC框架。
文件結(jié)構(gòu):
代碼:
MainActivity.java
package com.example.calculator; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView textView1,textView2,textView_op,textView_res; Calculator calculator; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1=(TextView) findViewById(R.id.textview_num1); textView2=(TextView)findViewById(R.id.textview_num2); textView_op=(TextView)findViewById(R.id.textview_op); textView_res=(TextView)findViewById(R.id.textview_res); Button t_C=(Button) findViewById(R.id.C); Button t_genhao=(Button) findViewById(R.id.genhao); Button t_quyu=(Button) findViewById(R.id.quyu); Button t_add=(Button) findViewById(R.id.add); Button t_num7=(Button) findViewById(R.id.num7); Button t_num8=(Button) findViewById(R.id.num8); Button t_num9=(Button) findViewById(R.id.num9); Button t_sub=(Button) findViewById(R.id.sub); Button t_num4=(Button) findViewById(R.id.num4); Button t_num5=(Button) findViewById(R.id.num5); Button t_num6=(Button) findViewById(R.id.num6); Button t_xinghao=(Button) findViewById(R.id.xinghao); Button t_num1=(Button) findViewById(R.id.num1); Button t_num2=(Button) findViewById(R.id.num2); Button t_num3=(Button) findViewById(R.id.num3); Button t_chuhao=(Button) findViewById(R.id.chuhao); Button t_jiahuojian=(Button) findViewById(R.id.jiahuojian); Button t_num0=(Button) findViewById(R.id.num0); Button t_dian=(Button) findViewById(R.id.dian); Button t_denhao=(Button) findViewById(R.id.denhao); calculator=new Calculator(); View.OnClickListener buttonlistener=new View.OnClickListener() { @Override public void onClick(View view) { String show=((Button)view).getText().toString(); calculator.process(show); textView1.setText(calculator.getSnum1()); textView_op.setText(calculator.getSop()); textView2.setText(calculator.getSnum2()); textView_res.setText(calculator.getSres()); } }; t_num0.setOnClickListener(buttonlistener); t_num1.setOnClickListener(buttonlistener); t_num2.setOnClickListener(buttonlistener); t_num3.setOnClickListener(buttonlistener); t_num4.setOnClickListener(buttonlistener); t_num5.setOnClickListener(buttonlistener); t_num6.setOnClickListener(buttonlistener); t_num7.setOnClickListener(buttonlistener); t_num8.setOnClickListener(buttonlistener); t_num9.setOnClickListener(buttonlistener); t_C.setOnClickListener(buttonlistener); t_genhao.setOnClickListener(buttonlistener); t_quyu.setOnClickListener(buttonlistener); t_jiahuojian.setOnClickListener(buttonlistener); t_add.setOnClickListener(buttonlistener); t_sub.setOnClickListener(buttonlistener); t_xinghao.setOnClickListener(buttonlistener); t_chuhao.setOnClickListener(buttonlistener); t_dian.setOnClickListener(buttonlistener); t_denhao.setOnClickListener(buttonlistener); } Calculator.java package com.example.calculator; import static java.math.BigDecimal.ROUND_HALF_UP; import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; public class Calculator { BigDecimal b1,b2,bres; boolean floatflag1,floatflag2; int scale; String snum1,snum2,sop,sres; enum state{state_i1,state_i2,state_init,state_res}; state s; //enum operator{op_add,op_sub,op_mul,op_div,op_none} //operator op; public Calculator(){ scale=100; floatflag1=false; floatflag2=false; clear(); } public void process(String show) { if(show.charAt(0)>='0'&& show.charAt(0)<='9')//輸入數(shù)字 { switch (s) { case state_init: if(show.charAt(0)=='0')break; snum1=show; s=state.state_i1; break; case state_i1: snum1+=show; break; case state_i2: snum2+=show; break; case state_res: clear(); process(show); break; } } else//輸入操作符 { switch (show) { case "C": clear(); break; case "+": switch (s) { case state_init: sop=show;s=state.state_i2; break; case state_i1: sop=show;s=state.state_i2; break; case state_i2: if(snum2=="") { sop=show; break; } snum1=getres(2);snum2="";sres="";sop=show; break; case state_res: if(sres.contains("error")) { clear(); break; } sop=show;snum1=sres;snum2="";sres="";s=state.state_i2; break; } break; case "=": switch (s) { case state_i1: if(snum1.charAt(snum1.length()-1)=='.')snum1+="0"; sres=snum1; snum1=""; snum2=""; sop=""; s=state.state_res; break; case state_i2: if(snum2=="")break; if(snum2.charAt(snum2.length()-1)=='.')break; sres=getres(2); snum1=""; snum2=""; sop=""; s=state.state_res; break; default:break; } break; case "-": switch (s) { case state_init: sop=show;s=state.state_i2; break; case state_i1: sop=show;s=state.state_i2; break; case state_i2: if(snum2=="") { sop=show; break; } snum1=getres(2);snum2="";sres="";sop=show; break; case state_res: if(sres.contains("error")) { clear(); break; } sop=show;snum1=sres;snum2="";sres="";s=state.state_i2; break; } break; case "*": switch (s) { case state_init: sop=show;s=state.state_i2; break; case state_i1: sop=show;s=state.state_i2; break; case state_i2: if(snum2=="") { sop=show; break; } snum1=getres(2);snum2="";sres="";sop=show; break; case state_res: if(sres.contains("error")) { clear(); break; } sop=show;snum1=sres;snum2="";sres="";s=state.state_i2; break; } break; case "/": switch (s) { case state_init: sop=show;s=state.state_i2; break; case state_i1: sop=show;s=state.state_i2; break; case state_i2: if(snum2=="") { sop=show; break; } snum1=getres(2);snum2="";sres="";sop=show; break; case state_res: if(sres.contains("error")) { clear(); break; } sop=show;snum1=sres;snum2="";sres="";s=state.state_i2; break; } break; case "√": switch (s) { case state_init: snum1="";snum2="";sop="";sres="0";s=state.state_res; break; case state_i1: sop=show;sres=getres(1);snum1="";snum2="";sop="";s=state.state_res; break; case state_i2: if(snum2=="")break; String t=snum1;snum1=snum2; String t_op=sop;sop=show; snum2=getres(1); snum1=t; sop=t_op; sres=""; break; case state_res: if(sres.contains("error")) { clear(); break; } sop=show; snum1=sres; sres=getres(1); snum1=""; sop=""; break; } break; case "%": switch (s) { case state_init: break; case state_i1: if(snum1.contains("."))break; sop=show;s=state.state_i2; break; case state_i2: if(snum2=="") { if(!snum1.contains(".")) sop=show; break; } snum1=getres(2);snum2="";sres="";sop=show; break; case state_res: if(sres.contains("error")) { clear(); break; } if(sres.contains("."))break; sop=show;snum1=sres;snum2="";sres="";s=state.state_i2; break; } break; case "+/-": switch (s) { case state_init: break; case state_i1: if(snum1.charAt(0)!='-') snum1="-"+snum1; else snum1=snum1.substring(1); break; case state_i2: if(snum2=="")break; if(snum2.charAt(0)!='-') snum2="-"+snum2; else snum2=snum2.substring(1); break; case state_res: if(sres.contains("error")) { clear(); break; } if(sres.charAt(0)!='-'&& !sres.equals(0)) sres="-"+sres; else sres=sres.substring(1); snum1=sres; snum2=""; sres=""; sop=""; if(snum1.equals("0")) s=state.state_init; else s=state.state_i1; break; } break; case ".": switch (s) { case state_init: snum1+=show; s=state.state_i1; break; case state_i1: if(snum1.contains("."))break; else snum1+=show; break; case state_i2: if(snum2.equals("")){ if(sop.equals("%"))break; snum2="0."; break; } if(sop.equals("%")||snum2.contains("."))break; else snum2+=show; break; case state_res: break; } break; } } } public String getSnum1() { return snum1; } public String getSnum2() { return snum2; } public String getSop() { return sop; } public String getSres() { return sres; } public void clear() { snum1="0"; snum2=""; sop=""; sres=""; s=state.state_init; b1=BigDecimal.valueOf(0); b2=BigDecimal.valueOf(0); bres=BigDecimal.valueOf(0); } public String getres(int flag) { String t="null"; if(flag==1) { b1=new BigDecimal(snum1); if(snum1.equals("0"))return t="0"; if(sop.equals("√")) { bres=sqrt(b1,scale); t=bres.toString(); return t; } else if(sop.equals("+/-")) { //略了,直接字符串處理 } } else if(flag==2) { b1=new BigDecimal(snum1); b2=new BigDecimal(snum2); switch (sop) { case "+": bres=b1.add(b2); t=bres.toString(); break; case "-": bres=b1.subtract(b2); t=bres.toString(); break; case "*": bres=b1.multiply(b2); t=bres.toString(); break; case "/": if(b2.equals(BigDecimal.valueOf(0)))return t="error: / by zero"; bres=b1.divide(b2,scale,RoundingMode.HALF_UP); t=bres.toString(); break; case "%": if(b2.equals(BigDecimal.valueOf(0)))return t="error: / by zero"; bres=b1.remainder(b2); t=bres.toString(); break; } } return t; } public static BigDecimal sqrt(BigDecimal value, int scale){ //高精度浮點(diǎn)數(shù)開(kāi)根號(hào) BigDecimal num2 = BigDecimal.valueOf(2); int precision = 100; MathContext mc = new MathContext(precision, RoundingMode.HALF_UP); BigDecimal deviation = value; int cnt = 0; while (cnt < precision) { deviation = (deviation.add(value.divide(deviation, mc))).divide(num2, mc); cnt++; } deviation = deviation.setScale(scale, BigDecimal.ROUND_HALF_UP); return deviation; } }
布局文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textview_num1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="0" android:textAlignment="textEnd" android:textColor="#009688" android:textSize="24sp" android:textStyle="bold" /> <TextView android:id="@+id/textview_op" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="" android:textAlignment="textEnd" android:textColor="#009688" android:textSize="24sp" android:textStyle="bold" /> <TextView android:id="@+id/textview_num2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="" android:textAlignment="textEnd" android:textColor="#009688" android:textSize="24sp" android:textStyle="bold" /> <TextView android:id="@+id/textview_res" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="" android:textAlignment="textEnd" android:textColor="#009688" android:textSize="24sp" android:textStyle="bold" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/C" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="C" /> <Button android:id="@+id/genhao" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="√" /> <Button android:id="@+id/quyu" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="%" /> <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="+" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/num7" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="7" /> <Button android:id="@+id/num8" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="8" /> <Button android:id="@+id/num9" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="9" /> <Button android:id="@+id/sub" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="-" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/num4" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="4" /> <Button android:id="@+id/num5" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="5" /> <Button android:id="@+id/num6" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="6" /> <Button android:id="@+id/xinghao" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="*" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/num1" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="1" /> <Button android:id="@+id/num2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="2" /> <Button android:id="@+id/num3" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="3" /> <Button android:id="@+id/chuhao" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="/" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/jiahuojian" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="+/-" /> <Button android:id="@+id/num0" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="0" /> <Button android:id="@+id/dian" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="." /> <Button android:id="@+id/denhao" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="=" /> </LinearLayout> </LinearLayout>
到此這篇關(guān)于android studio 項(xiàng)目 :UI設(shè)計(jì)高精度實(shí)現(xiàn)簡(jiǎn)單計(jì)算器的文章就介紹到這了,更多相關(guān)UI設(shè)計(jì)高精度實(shí)現(xiàn)簡(jiǎn)單計(jì)算器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android?Studio實(shí)現(xiàn)帶三角函數(shù)對(duì)數(shù)運(yùn)算功能的高級(jí)計(jì)算器
- Android Studio實(shí)現(xiàn)簡(jiǎn)易進(jìn)制轉(zhuǎn)換計(jì)算器
- android?studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器小功能
- Android studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器App功能
- 用Android?studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
- Android?Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器設(shè)計(jì)
- Android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器
- Android Studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器(表格布局TableLayout)
- Android?Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器App?(Java語(yǔ)言版)
相關(guān)文章
Android Studio出現(xiàn)Failed to pull selection: open failed: Permi
本篇文章給大家分享了Android Studio中導(dǎo)出數(shù)據(jù)庫(kù)文件的方法以及出現(xiàn)Failed to pull selection: open failed: Permission denied的解決思路,有興趣的學(xué)習(xí)下。2018-05-05Jetpack?Compose入門(mén)基礎(chǔ)全面精講
開(kāi)始布局部分。這部分我個(gè)人感覺(jué)沒(méi)有必要每個(gè)組件、屬性都詳細(xì)說(shuō)到,否則篇幅會(huì)很長(zhǎng)。建立起Compose中的組件與?Android?Views的一個(gè)對(duì)應(yīng)關(guān)系就夠了。具體還是需要在實(shí)際的使用中去熟悉2022-10-10Android 回調(diào)詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android 回調(diào)詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01Android 解決WebView無(wú)法上傳文件的問(wèn)題
這篇文章主要介紹了Android 解決WebView無(wú)法上傳文件的問(wèn)題的相關(guān)資料,需要的朋友可以參考下2017-07-07Android開(kāi)發(fā)仿IOS滑動(dòng)開(kāi)關(guān)實(shí)現(xiàn)代碼
這篇文章主要介紹了 android開(kāi)發(fā)仿IOS滑動(dòng)開(kāi)關(guān)實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05android實(shí)現(xiàn)短按電源鍵關(guān)機(jī)的實(shí)現(xiàn)代碼
這篇文章主要介紹了android實(shí)現(xiàn)短按電源鍵關(guān)機(jī)的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Retrofit實(shí)現(xiàn)圖文上傳至服務(wù)器
本文主要介紹了Retrofit實(shí)現(xiàn)圖文上傳至服務(wù)器的相關(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-03-03Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話(huà)框的方法
這篇文章主要介紹了Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話(huà)框的方法,涉及設(shè)置監(jiān)聽(tīng)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02