android之简易计算器,任意按都不会报错,修改之https://www.cnblogs.com/whongs/p/7452001.html
android之简易计算器,任意按都不会报错,修改之https://www.cnblogs.com/whongs/p/7452001.html的文章,修改原因,某些情况下程序会崩溃。
layout文件如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_ji_suan02" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.admin.simplecalc.MainActivity"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="40dp" android:textSize="30dp" android:text="0" android:textColor="#ff0000" android:gravity="center_vertical|right" android:layout_marginRight="5dp" android:layout_marginLeft="5dp" android:background="#FFFF00" /> <!--7 8 9 + --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="5dp" android:orientation="horizontal" > <Button android:id="@+id/Button07" android:text="7" android:textSize="25dp" android:layout_width="80dp" android:layout_height="wrap_content" /> <Button android:id="@+id/Button08" android:text="8" android:textSize="25dp" android:layout_width="80dp" android:layout_height="wrap_content" /> <Button android:id="@+id/Button09" android:text="9" android:textSize="25dp" android:layout_width="80dp" android:layout_height="wrap_content" /> <Button android:id="@+id/ButtonJia" android:text="+" android:textSize="25dp" android:layout_width="80dp" android:layout_height="wrap_content" /> </LinearLayout> <!--4 5 6 - --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="5dp" android:orientation="horizontal" > <Button android:id="@+id/Button04" android:text="4" android:textSize="25dp" android:layout_width="80dp" android:layout_height="wrap_content" /> <Button android:id="@+id/Button05" android:text="5" android:textSize="25dp" android:layout_width="80dp" android:layout_height="wrap_content" /> <Button android:id="@+id/Button06" android:text="6" android:textSize="25dp" android:layout_width="80dp" android:layout_height="wrap_content" /> <Button android:id="@+id/ButtonJian" android:text="-" android:textSize="25dp" android:layout_width="80dp" android:layout_height="wrap_content" /> </LinearLayout> <!--1 2 3 * --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="5dp" android:orientation="horizontal" > <Button android:id="@+id/Button01" android:text="1" android:textSize="25dp" android:layout_width="80dp" android:layout_height="wrap_content" /> <Button android:id="@+id/Button02" android:text="2" android:textSize="25dp" android:layout_width="80dp" android:layout_height="wrap_content" /> <Button android:id="@+id/Button03" android:text="3" android:textSize="25dp" android:layout_width="80dp" android:layout_height="wrap_content" /> <Button android:id="@+id/ButtonCheng" android:text="*" android:textSize="25dp" android:layout_width="80dp" android:layout_height="wrap_content" /> </LinearLayout> <!--0 C = / --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="5dp" android:orientation="horizontal" > <Button android:id="@+id/Button00" android:text="0" android:textSize="25dp" android:layout_width="80dp" android:layout_height="wrap_content" /> <Button android:id="@+id/ButtonC" android:text="C" android:textSize="25dp" android:layout_width="80dp" android:layout_height="wrap_content" /> <Button android:id="@+id/ButtonDeng" android:text="=" android:textSize="25dp" android:layout_width="80dp" android:layout_height="wrap_content" /> <Button android:id="@+id/ButtonChu" android:text="/" android:textSize="25dp" android:layout_width="80dp" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>java代码如下:
package com.example.admin.simplecalc; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { TextView tv; int[] buttons; //数字按钮数组 int result; int result0; int result1; //按钮对象声明 Button buttonC; Button buttonJia; Button buttonJian; Button buttonCheng; Button buttonChu; Button buttonDengyu; String str1; //旧输入的值 String str2; //新输入的值 int flag = 0; //计算标志位,0第一次输入;1加; 2减; 3乘; 4除; 5等于 Button temp; int flagbutton = 0; int idflag= 0; int calcflag= 0; int numflag= 0; int equflag= 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initButton(); //清空按钮点击事件 buttonC.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { str1 = ""; str2 = ""; tv.setText("0"); result = 0; result1 = 0; result0 = 0; flag = 0; flagbutton = 0; idflag =0; calcflag = 0; numflag = 0; equflag = 0; } }); //监听 for (int i = 0; i < buttons.length; i++){ temp = getBtnForId(buttons[i]); temp.setOnClickListener(new View.OnClickListener() { //tv.setText(""); @Override public void onClick(View view) { if (flag != 0){ str2 = ""; if(numflag ==1){ numflag = 0; tv.setText(""); calcflag = 0; } } else { if(flagbutton ==0){ tv.setText(""); flagbutton++; } str2 = tv.getText().toString().trim(); if (str2.equals("0")) { str2 = ""; } } str2 = tv.getText().toString().trim(); str2 = str2 + String.valueOf(((Button)view).getText()); //获取新值 /* if(idflag ==1){ str2 =str2+str1; } */ tv.setText(str2); } }); } buttonListener(buttonJia, 1); buttonListener(buttonJian, 2); buttonListener(buttonCheng, 3); buttonListener(buttonChu, 4); buttonDengyu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //str2 = tv.getText().toString().trim(); //str2 = str2 + String.valueOf(((Button)view).getText()); //result1 = Integer.parseInt(str2); if(equflag == 0){ equflag = 1; switch (flag) { case 0: result = result1; break; case 1: str1 = str2; str2.indexOf("+"); str2=str2.substring(str2.indexOf("+")+1); result1 = Integer.parseInt(str2); result = result0 + result1; break; case 2: str1 = str2; str2.indexOf("-"); str2=str2.substring(str2.indexOf("-")+1); result1 = Integer.parseInt(str2); result = result0 - result1; break; case 3: str1 = str2; str2.indexOf("*"); str2=str2.substring(str2.indexOf("*")+1); result1 = Integer.parseInt(str2); result = result0 * result1; break; case 4: str1 = str2; str2.indexOf("/"); str2 = str2.substring(str2.indexOf("/")+1); result1 = Integer.parseInt(str2); if (result1 == 0){ Toast.makeText(MainActivity.this, "除数不能为0", Toast.LENGTH_SHORT).show(); }else { result = result0 / result1; } break; } String str = (str1 +"="+ result + "").trim(); if (result1 == 0 && flag == 4){ str = "错误"; } tv.setText(str); Toast.makeText(MainActivity.this, "结果是:" + result, Toast.LENGTH_SHORT).show(); str2 = ""; str1= ""; idflag = 1; numflag= 1; } } }); } //初始化控件资源 public void initButton(){ tv = (TextView)this.findViewById(R.id.tv); str1 = String.valueOf(tv.getText()); str2 = ""; buttonC = getBtnForId(R.id.ButtonC); buttonJia = getBtnForId(R.id.ButtonJia); buttonJian = getBtnForId(R.id.ButtonJian); buttonCheng = getBtnForId(R.id.ButtonCheng); buttonChu = getBtnForId(R.id.ButtonChu); buttonDengyu = getBtnForId(R.id.ButtonDeng); buttons = new int[]{ R.id.Button00,R.id.Button01,R.id.Button02, R.id.Button03, R.id.Button04,R.id.Button05,R.id.Button06, R.id.Button07,R.id.Button08,R.id.Button09 }; } //根据id获取Button public Button getBtnForId(int rID){ Button btn = (Button)this.findViewById(rID); return btn; } //按钮监听 public void buttonListener(Button button, final int id){ button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (calcflag == 0) { calcflag = 1; String strtemp; strtemp = tv.getText().toString().trim(); //str = str + String.valueOf(((Button)view).getText()); result0 = Integer.parseInt(strtemp); if (strtemp.equals("0") || strtemp.equals(" ")){ }else{ equflag = 0; //str1=strtemp; switch (id) { case 1: tv.setText(strtemp + "+"); str1 = strtemp; flag = id; flagbutton = 0; break; case 2: tv.setText(strtemp + "-"); str1 = strtemp; flag = id; flagbutton = 0; break; case 3: tv.setText(strtemp + "*"); str1 = strtemp; flag = id; flagbutton = 0; break; case 4: tv.setText(strtemp + "/"); str1 = strtemp; flag = id; flagbutton = 0; break; } } } } }); } }