【基本Android计算器】千金一刻莫空度,老大无成空自伤。这篇文章主要讲述基本Android计算器相关的知识,希望能为你提供帮助。
This is a current side project. I make changes (mainly adding new things) about once a week (when I have time).
Notes: GUI in XML(not here), Eclipse android plug-in, some images not here
- package newProject.test;
- //Code by ehrenb
- import android.app.Activity;
- import android.os.Bundle;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.Window;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.RadioButton;
- import android.widget.Toast;
- import java.util.ArrayList;
- import android.widget.*;
- //import java.util.ArrayList;
- //needs error checking for number parsing**
- public class myMenu extends Activity {
- private EditText inputNums;
- private TextView resultBox;
- @Override
- public void onCreate( Bundle savedInstanceState) {
- super.onCreate( savedInstanceState) ;
- setContentView( R.layout.main) ;
- inputNums = ( EditText) findViewById( R.id.input) ;
- resultBox = ( TextView) findViewById( R.id.results) ;
- // requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
- }
- //int[] storage = new int[10];
- //ArrayList< Double> storage=new ArrayList< Double> ();
- double numToCalc=0;
- // This method is called at button click because we assigned the name to the
- // " On Click property" of the button
- public void myClickHandler( View view) {
- switch ( view.getId( ) ) {
- }
- //initialize all the buttons
- Button addButton = ( Button) findViewById( R.id.addButton) ;
- Button multButton = ( Button) findViewById( R.id.multButton) ;
- Button subtractButton = ( Button) findViewById( R.id.subtract) ;
- Button divideButton = ( Button) findViewById( R.id.divideButton) ;
- Button factorsButton = ( Button) findViewById( R.id.factorsButton) ;
- if ( inputNums.getText( ) .length( ) == 0) {
- Toast.makeText( this, " Please enter a valid number" ,
- Toast.LENGTH_LONG) .show( ) ;
- return; //return is a MUST...force close without
- //parse for numbers, not just a blank input
- }
- //Code by ehrenb
- // check for non-numeric values
- //abstracted from:
- //src = http://www.ehow.com/how_7718014_error-check-fields-java.html
- for( int i= 0; i < inputNums.getText( ) .length( ) ; i++) {
- if( !Character.isDigit( inputNums.getText( ) .charAt( i) ) )
- Toast.makeText( this, " Please enter a valid number" ,
- Toast.LENGTH_LONG) .show( ) ;
- return;
- }
- double inputValue = https://www.songbingjia.com/android/Double.parseDouble( inputNums.getText( ) .toString( ) ) ;
- if ( addButton.isPressed( ) ) {
- //storage.add(inputValue); //add parsed double to arraylist
- //numToCalc = inputValue;
- //inputNums.setText(inputValue.);
- //inputNums.append(" +" ); //add + to the textView
- //permNum=add(inputValue);
- resultBox.setText( String
- .valueOf( add( inputValue) ) ) ; //do addition calculations
- inputNums.setText( " " ) ; //clear the textfield
- }
- if ( multButton.isPressed( ) ) {
- numToCalc = inputValue;
- //add parsed double to arraylist
- //inputNums.setText(inputValue.);
- //inputNums.append(" +" ); //add + to the textView
- //storage.add(inputValue);
- //storage.add(inputValue);
- //permNum=resultValue;
- resultBox.setText( String
- .valueOf( multiply( inputValue) ) ) ; //do addition calculations
- inputNums.setText( " " ) ; //clear the textfield
- }
- if( subtractButton.isPressed( ) ) {
- resultBox.setText( String.valueOf( subtract( inputValue) ) ) ; //do addition calculations
- inputNums.setText( " " ) ; //clear the textfield
- }
- // Switch to the other button
- //if (fahrenheitButton.isChecked()) {
- //fahrenheitButton.setChecked(false);
- //celsiusButton.setChecked(true);
- //} else {
- //fahrenheitButton.setChecked(true);
- //celsiusButton.setChecked(false);
- //}
- if ( divideButton.isPressed( ) ) {
- numToCalc = inputValue;
- resultBox.setText( String
- .valueOf( divide( inputValue) ) ) ; //do division calculations
- inputNums.setText( " " ) ; //clear the textfield
- }
- if ( factorsButton.isPressed( ) ) {
- resultBox.setText( String.valueOf( findFactors( inputValue) ) ) ;
- }
- }
- // add
- int numOfAdds=0;
- private double add( double addNum) {
- //int temp = storage.size();
- double total =0;
- //if (temp == 0) // if array is empty, do nothing
- //{
- //
- //}
- //else
- {
- double resultValue = https://www.songbingjia.com/android/Double.parseDouble( resultBox.getText( ) .toString( ) ) ;
- double inputValue = https://www.songbingjia.com/android/Double.parseDouble( inputNums.getText( ) .toString( ) ) ;
- // total=numToCalc+resultValue;
- total = addNum+resultValue;
- //permNum=total;
- //for (int i=0; i< temp; i++) //loop adds up every item in the array
- //{
- //total = total +storage.get(i);
- //
- //}
- //storage.clear(); //standard template for calc functions: clear the arrayList, then add
- //storage.add(total);
- }
- numOfAdds++;
- return ( total) ;
- }
- int multiplicationInit=1;
- int numOfMultis=0; //FIX - counts amt of multiplications done in the program
- private double multiply( double multiplyNum)
- {
- double inputValue = https://www.songbingjia.com/android/Double.parseDouble( inputNums.getText( ) .toString( ) ) ;
- double resultValue = https://www.songbingjia.com/android/Double.parseDouble( resultBox.getText( ) .toString( ) ) ;
- double product=0;
- product=multiplyNum*resultValue;
- if ( numOfMultis==0 & & resultValue=https://www.songbingjia.com/android/=0 & & numOfAdds==0 & & numOfSubtract==0& & numOfDivide==0)
- //if it is absolutely the first calculation < < FIX!! If it is BOTH: The first Calculation & the resultBox=0 > it is ABSOLUTELY the first calculation.
- //previous problem: if(numOfMultis==0) only checks to see when the first multiplication is, not what the values are
- //still testing
- //seek to optimize this later--IE- no number of calculations counted
- {
- product = multiplyNum*multiplicationInit; //workaround
- }
- else //assuming we have already done something to the resultValue
- //then we dont need to worry about the case of multiplying product*0
- {
- product = multiplyNum*resultValue; //multiply normally
- }
- numOfMultis++;
- return product;
- }
- int numOfSubtract=0;
- int numOfDivide=0;
- private double subtract( double subtractNum)
- {
- double total =0;
- double resultValue = https://www.songbingjia.com/android/Double.parseDouble( resultBox.getText( ) .toString( ) ) ;
- total=resultValue-subtractNum;
- numOfSubtract++;
- return total;
- }
- private double divide( double divideNum)
- {
- double resultValue = https://www.songbingjia.com/android/Double.parseDouble( resultBox.getText( ) .toString( ) ) ;
- numOfDivide++;
- return resultValue/divideNum;
- }
- private String findFactors( double factNum)
- {
- ArrayList tempArray = new ArrayList( ) ;
- for( int i = 1; i < = factNum/2; i++)
- {
- if( factNum % i == 0) // if the remainder of num5 and a number is 0..it is a factor!
- tempArray.add( i) ;
- //System.out.print(i + " " );
- }
- return tempArray.toString( ) ;
- }
- //MULTIPLCATION NOTES:
- //**Calculations are done off of the resultValue
- //Since the result value has to initially be 0, we must compensate
- //and make resultValue=https://www.songbingjia.com/android/1 in the case that the user multiplies first
- //instead of add/sub/div
- //If we were to keep resultValue=https://www.songbingjia.com/android/0, we would run into a problem where; if multiplying first(assuming resultValue hasnt been touched), our calculation would be forced to =0
- // because by default, resultValue must=0. IE 1*0=0
- //goals:maintain universal resultValue
- //problems: if subtracting say 3-3=0. If we attempt to multiply after that, we will encounter an issue b/c it will force the 0 to be a 1.
- //Solution: compensate again in the functions that require it (force 1 to be a 0?)
- //above problem fixed
- //Code by ehrenb
- }
推荐阅读
- Android表布局宽度问题
- 用户€IIS APPPOOLDefaultAppPool€登录失败?
- Android-底部有标签
- 在Android中使用getView和convertView
- [android]全屏
- [android]加载新视图
- [android]无边框Tabbar底部
- [android]设置超时
- Swift中面向协议编程的简介