安卓简易计算器
安卓
Android是由Andy Rubin創(chuàng)立的一個(gè)手機(jī)操作系統(tǒng),后來(lái)被Google收購(gòu)。Gooole希望與各方面共同建立一個(gè)標(biāo)準(zhǔn)化的、開(kāi)放式的移動(dòng)電話軟件平臺(tái),從而在移動(dòng)產(chǎn)業(yè)形成一個(gè)開(kāi)放式的操作平臺(tái)。
界面編程
安卓常用的幾種布局:線性布局(LineraLayout)、表格布局(Tablelayout)、幀布局(FrameLayout)、相對(duì)布局(RelativeLayout)等等。我這里用的是比較簡(jiǎn)單的“線性布局”。
布局文件(activity_main.xml):
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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="com.example.zhaoxudong.calculatorapp.MainActivity"> ? <LinearLayout ? ? ? android:layout_width="wrap_content" ? ? ? android:layout_height="wrap_content" ? ? ? android:orientation="vertical" ? ? ? tools:layout_editor_absoluteY="0dp" ? ? ? tools:layout_editor_absoluteX="0dp"> ? ? ? ? <EditText ? ? ? ? ? android:layout_width="403dp" ? ? ? ? ? android:layout_height="155dp" ? ? ? ? ? android:id="@+id/result_Text"/> ? ? ? <LinearLayout ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? android:orientation="horizontal"> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="CE" ? ? ? ? ? ? ? android:id="@+id/CE"/> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="DEL" ? ? ? ? ? ? ? android:id="@+id/DEL"/> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="SIN" ? ? ? ? ? ? ? android:id="@+id/SIN"/> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="113dp" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="COS" ? ? ? ? ? ? ? android:id="@+id/COS"/> ? ? ? ? ? ? </LinearLayout> ? ? ? ? <LinearLayout ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? android:orientation="horizontal"> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="7" ? ? ? ? ? ? ? android:id="@+id/number7"/> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="8" ? ? ? ? ? ? ? android:id="@+id/number8"/>/> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="9" ? ? ? ? ? ? ? android:id="@+id/number9"/>/> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="117dp" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="+" ? ? ? ? ? ? ? android:id="@+id/add_but"/> ? ? ? </LinearLayout> ? ? ? ? <LinearLayout ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? android:orientation="horizontal"> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="4" ? ? ? ? ? ? ? android:id="@+id/number4"/>/> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="5" ? ? ? ? ? ? ? android:id="@+id/number5"/>/> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="6" ? ? ? ? ? ? ? android:id="@+id/number6"/>/> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="117dp" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="-" ? ? ? ? ? ? ? android:id="@+id/sub_but"/> ? ? ? </LinearLayout> ? ? ? ? <LinearLayout ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? android:orientation="horizontal"> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="1" ? ? ? ? ? ? ? android:id="@+id/number1"/>/> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="2" ? ? ? ? ? ? ? android:id="@+id/number2"/>/> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="3" ? ? ? ? ? ? ? android:id="@+id/number3"/>/> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="116dp" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="*" ? ? ? ? ? ? ? android:id="@+id/mult_but"/> ? ? ? </LinearLayout> ? ? ? ? <LinearLayout ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? android:orientation="horizontal"> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="=" ? ? ? ? ? ? ? android:id="@+id/sign_but"/> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="0" ? ? ? ? ? ? ? android:id="@+id/number0"/>/> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="." ? ? ? ? ? ? ? android:id="@+id/spot_but"/> ? ? ? ? ? <Button ? ? ? ? ? ? ? android:layout_width="114dp" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:text="/" ? ? ? ? ? ? ? android:id="@+id/div_but"/> ? ? ? </LinearLayout> ? ? </LinearLayout> ? ? </android.support.constraint.ConstraintLayout> ?界面效果:
邏輯部分
邏輯部分是由java代碼實(shí)現(xiàn),實(shí)踐證明代碼多練還是有好處的。
為了實(shí)現(xiàn)按鈕的點(diǎn)擊事件:
public class MainActivity extends Activity implements OnClikListener{}對(duì)layout中的按鈕進(jìn)行定義:
private Button number0; ? private Button number1; ? private Button number2; ? private Button number3; ? private Button number4; ? private Button number5; ? private Button number6; ? private Button number7; ? private Button number8; ? private Button number9; ? ? /* ? 運(yùn)算符 ? ? */ ? private Button add_but;//加法 ? private Button sub_but;//減法 ? private Button mult_but;//乘法 ? private Button div_but;//除法 ? ? /* ? 等號(hào) ? 小數(shù)點(diǎn) ? 歸零 ? ? */ ? private Button sign_but;//等號(hào) ? private Button spot_but;//小數(shù)點(diǎn) ? private Button CE_but;//歸零鍵 ? private Button DEL; ? private Button SIN; ? private Button COS; ? /* ? 結(jié)果 ? ? */ ? private EditText result; ? private boolean clear_flag;//清空標(biāo)識(shí) ?對(duì)每個(gè)按鈕進(jìn)行實(shí)例化并且設(shè)置點(diǎn)擊事件
@Override ? protected void onCreate(Bundle savedInstanceState) { ? ? ? super.onCreate(savedInstanceState); ? ? ? setContentView(R.layout.activity_main); ? ? ? /* ? ? ? 數(shù)字 ? ? ? ? */ ? ? ? number0 = (Button) findViewById(R.id.number0); ? ? ? number1 = (Button) findViewById(R.id.number1); ? ? ? number2 = (Button) findViewById(R.id.number2); ? ? ? number3 = (Button) findViewById(R.id.number3); ? ? ? number4 = (Button) findViewById(R.id.number4); ? ? ? number5 = (Button) findViewById(R.id.number5); ? ? ? number6 = (Button) findViewById(R.id.number6); ? ? ? number7 = (Button) findViewById(R.id.number7); ? ? ? number8 = (Button) findViewById(R.id.number8); ? ? ? number9 = (Button) findViewById(R.id.number9); ? ? ? /* ? ? ? 運(yùn)算符 ? ? ? ? */ ? ? ? add_but = (Button) findViewById(R.id.add_but); ? ? ? sub_but = (Button) findViewById(R.id.sub_but); ? ? ? mult_but = (Button) findViewById(R.id.mult_but); ? ? ? div_but = (Button) findViewById(R.id.div_but); ? ? ? /* ? ? ? 等號(hào) ? ? ? 小數(shù)點(diǎn) ? ? ? 歸零 ? ? ? ? */ ? ? ? sign_but = (Button) findViewById(R.id.sign_but);//等號(hào) ? ? ? spot_but = (Button) findViewById(R.id.spot_but);//小數(shù)點(diǎn) ? ? ? CE_but = (Button) findViewById(R.id.CE);//歸零 ? ? ? DEL = (Button) findViewById(R.id.DEL); ? ? ? SIN=(Button) findViewById(R.id.SIN); ? ? ? COS=(Button) findViewById(R.id.COS); ? ? ? /* ? ? ? 結(jié)果 ? ? ? ? */ ? ? ? result = (EditText) findViewById(R.id.result_Text); ? ? ? /* ? 初始化事件 ? ? */ ? ? ? ? number0.setOnClickListener(this); ? ? ? number1.setOnClickListener(this); ? ? ? number2.setOnClickListener(this); ? ? ? number3.setOnClickListener(this); ? ? ? number4.setOnClickListener(this); ? ? ? number5.setOnClickListener(this); ? ? ? number6.setOnClickListener(this); ? ? ? number7.setOnClickListener(this); ? ? ? number8.setOnClickListener(this); ? ? ? number9.setOnClickListener(this); ? ? ? ? add_but.setOnClickListener(this); ? ? ? sub_but.setOnClickListener(this); ? ? ? mult_but.setOnClickListener(this); ? ? ? div_but.setOnClickListener(this); ? ? ? ? sign_but.setOnClickListener(this); ? ? ? spot_but.setOnClickListener(this); ? ? ? CE_but.setOnClickListener(this); ? ? ? DEL.setOnClickListener(this); ? ? ? SIN.setOnClickListener(this); ? ? ? COS.setOnClickListener(this); ? }下面是在onClick方法中實(shí)現(xiàn)邏輯運(yùn)算:
這里使用的方法是使用以下代碼獲取EditText中的字符串
String str = result.getText().toString();為了區(qū)分開(kāi)數(shù)字和運(yùn)算符,分別用s1、s2和op來(lái)表示數(shù)字和運(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);//運(yùn)算符后面的字符串為了能夠?qū)崿F(xiàn)點(diǎn)擊任意按鈕可以出現(xiàn)對(duì)應(yīng)的字符,使用以下代碼可以獲取Button中的內(nèi)容
result.setText(str + " " + ((Button) v).getText() + " ");通過(guò)判斷出數(shù)字和運(yùn)算符來(lái)進(jìn)行運(yùn)算,下面是邏輯運(yùn)算全部的代碼:
private void getResult() { ? ? ? clear_flag = true; ? ? ? String exp = result.getText().toString(); ? ? ? double result_1 = 0; ? ? ? if(exp==null||exp.equals("")){ ? ? ? ? ? return; ? ? ? } ? ? ? if(!exp.contains(" ")){ ? ? ? ? ? return; ? ? ? } ? ? ? if (clear_flag){ ? ? ? ? ? clear_flag=false; ? ? ? } ? ? ? clear_flag=true; ? ? ? 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);//運(yùn)算符后面的字符串 ? ? ? if (!s1.equals("") && !s2.equals("")) { ? ? ? ? ? double d1 = Double.parseDouble(s1); ? ? ? ? ? double d2 = Double.parseDouble(s2); ? ? ? ? ? if (op.equals("+")) { ? ? ? ? ? ? ? result_1 = d1 + d2; ? ? ? ? ? } else if (op.equals("-")) { ? ? ? ? ? ? ? result_1 = d1 - d2; ? ? ? ? ? ? } else if (op.equals("*")) { ? ? ? ? ? ? ? result_1 = d1 * d2; ? ? ? ? ? ? } else if (op.equals("/")) { ? ? ? ? ? ? ? if (d2 == 0) ? ? ? ? ? ? ? ? ? result_1 = 0; ? ? ? ? ? ? ? result_1 = d1 / d2; ? ? ? ? ? ? } ? ? ? ? ? if (!s1.contains(".") && !s2.contains(".")&&!op.equals("/")) { ? ? ? ? ? ? ? int r = (int) result_1; ? ? ? ? ? ? ? result.setText(r + ""); ? ? ? ? ? ? } else { ? ? ? ? ? ? ? result.setText(result_1 + ""); ? ? ? ? ? } ? ? ? ? } else if (!s1.equals("") && s2.equals("")) { ? ? ? ? ? result.setText(exp); ? ? ? } else if (s1.equals("") && !s2.equals("")) { ? ? ? ? ? double d2 = Double.parseDouble(s2); ? ? ? ? ? if (op.equals("+")) { ? ? ? ? ? ? ? result_1 = 0 + d2; ? ? ? ? ? } else if (op.equals("-")) { ? ? ? ? ? ? ? result_1 = 0 - d2; ? ? ? ? ? ? } else if (op.equals("*")) { ? ? ? ? ? ? ? result_1 = 0; ? ? ? ? ? ? } else if (op.equals("/")) { ? ? ? ? ? ? ? result_1 = 0; ? ? ? ? ? ? } ? ? ? } else { ? ? ? ? ? result.setText(""); ? ? ? } ? }以上便是簡(jiǎn)單計(jì)算器的所有重要代碼。
實(shí)現(xiàn)功能
支持加減乘除的基本運(yùn)算
支持加減乘除后的再運(yùn)算
支持浮點(diǎn)數(shù)運(yùn)算
支持三角函數(shù)sin cos運(yùn)算
支持?jǐn)?shù)字刪除
支持置零
總結(jié)
- 上一篇: 2022-2027年中国服装定制行业市场
- 下一篇: scary用法