Android development implementation calculator example, android calculator example

Source: Internet
Author: User

Android development implementation calculator example, android calculator example

Example

The Code is as follows:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/warpcontent"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">

<HorizontalScrollView
Android: id = "@ + id/scrollview"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">

<TextView
Android: id = "@ + id/resultText"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "right"
Android: gravity = "right"
Android: padding = "8dp"
Android: paddingLeft = "10dp"
Android: paddingRight = "10dp"
Android: text = "@ string/number0"
Android: textSize = "40sp"/>
</HorizontalScrollView>

<TableLayout
Android: id = "@ + id/buttonTable"
Android: layout_width = "fill_parent"
Android: layout_height = "0dp"
Android: layout_weight = "1"
Android: stretchColumns = "*">

<TableRow
Android: layout_width = "fill_parent"
Android: layout_height = "0dp"
Android: layout_weight = "1">

<Button
Android: id = "@ + id/button7"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: onClick = "onClickListener"
Android: text = "@ string/number7"/>

<Button
Android: id = "@ + id/button8"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: onClick = "onClickListener"
Android: text = "@ string/number8"/>

<Button
Android: id = "@ + id/button9"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: onClick = "onClickListener"
Android: text = "@ string/number9"/>

<Button
Android: id = "@ + id/buttonBackSpace"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: onClick = "onClickListener"
Android: text = "@ string/backspace"/>

<Button
Android: id = "@ + id/buttonClear"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: onClick = "onClickListener"
Android: text = "@ string/clear"/>
</TableRow>

<TableRow
Android: layout_width = "fill_parent"
Android: layout_height = "0dp"
Android: layout_weight = "1">

<Button
Android: id = "@ + id/button4"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: onClick = "onClickListener"
Android: text = "@ string/number4"/>

<Button
Android: id = "@ + id/button5"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: onClick = "onClickListener"
Android: text = "@ string/number5"/>

<Button
Android: id = "@ + id/button6"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: onClick = "onClickListener"
Android: text = "@ string/number6"/>

<Button
Android: id = "@ + id/buttonMultiply"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: onClick = "onClickListener"
Android: text = "@ string/multiply"/>

<Button
Android: id = "@ + id/buttonDivide"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: onClick = "onClickListener"
Android: text = "@ string/divide"/>
</TableRow>
</TableLayout>

<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "0dp"
Android: layout_weight = "1"
Android: orientation = "horizontal">

<TableLayout
Android: layout_width = "0dp"
Android: layout_height = "fill_parent"
Android: layout_weight = "3"
Android: stretchColumns = "*">

<TableRow
Android: layout_width = "fill_parent"
Android: layout_height = "0dp"
Android: layout_weight = "1">

<Button
Android: id = "@ + id/button1"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: onClick = "onClickListener"
Android: text = "@ string/number1"/>

<Button
Android: id = "@ + id/button2"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: onClick = "onClickListener"
Android: text = "@ string/number2"/>

<Button
Android: id = "@ + id/button3"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: onClick = "onClickListener"
Android: text = "@ string/number3"/>
</TableRow>

<TableRow
Android: layout_width = "fill_parent"
Android: layout_height = "0dp"
Android: layout_weight = "1">

<Button
Android: id = "@ + id/button0"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: onClick = "onClickListener"
Android: text = "@ string/number0"/>

<Button
Android: id = "@ + id/buttonDot"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: onClick = "onClickListener"
Android: text = "@ stri (www.111cn.net) ng/dot"/>

<Button
Android: id = "@ + id/buttonNegSign"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: onClick = "onClickListener"
Android: text = "@ string/negSign"/>
</TableRow>
</TableLayout>

<Button
Android: id = "@ + id/buttonPlus"
Android: layout_width = "0dp"
Android: layout_height = "fill_parent"
Android: layout_weight = "1"
Android: onClick = "onClickListener"
Android: text = "@ string/plus"/>

<LinearLayout
Android: layout_width = "0dp"
Android: layout_height = "fill_parent"
Android: layout_weight = "1"
Android: orientation = "vertical">

<Button
Android: id = "@ + id/buttonMinus"
Android: layout_width = "fill_parent"
Android: layout_height = "0dp"
Android: layout_weight = "1"
Android: onClick = "onClickListener"
Android: text = "@ string/minus"/>

<Button
Android: id = "@ + id/buttonEval"
Android: layout_width = "fill_parent"
Android: layout_height = "0dp"
Android: layout_weight = "1"
Android: onClick = "onClickListener"
Android: text = "@ string/eval"/>
</LinearLayout>
</LinearLayout>

</LinearLayout>

The MainActivity class is similar to the previous java calculator:

The Code is as follows:
Package me. jerrys. calc;

Import java. math. BigDecimal;

Import android. support. v7.app. ActionBarActivity;
Import android. app. AlertDialog;
Import android. OS. Bundle;
Import android. view. Menu;
Import android. view. MenuItem;
Import android. view. View;
Import android. widget. TextView;
Import android. widget. Toast;

Public class MainActivity extends ActionBarActivity {
Private TextView textView;
Private BigDecimal lastNum = new BigDecimal ("0 ");
Private BigDecimal currentNum = new BigDecimal ("0 ");
Private String currentText;
Private boolean typeClean = false;
Private boolean hadCalced = false;
Private MODE currentMode = MODE. PLUS;
Private enum MODE {
PLUS, MINUS, MULTIPLY, DIVIDE
}

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
TextView = (TextView) findViewById (R. id. resultText );
}

@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}

Public void onClickListener (View v ){
CurrentText = textView. getText (). toString ();
Switch (v. getId ()){
Case R. id. button0:
AddNumber ("0 ");
Break;
Case R. id. button1:
AddNumber ("1 ");
Break;
Case R. id. button2:
AddNumber ("2 ");
Break;
Case R. id. button3:
AddNumber ("3 ");
Break;
Case R. id. button4:
AddNumber ("4 ");
Break;
Case R. id. button5:
AddNumber ("5 ");
Break;
Case R. id. button6:
AddNumber ("6 ");
Break;
Case R. id. button7:
AddNumber ("7 ");
Break;
Case R. id. button8:
AddNumber ("8 ");
Break;
Case R. id. button9:
AddNumber ("9 ");
Break;
Case R. id. buttonNegSign:
AddSign ();
Break;
Case R. id. buttonClear:
Clear ();
Break;
Case R. id. buttonBackSpace:
BackSpace ();
Break;
Case R. id. buttonPlus:
SetMode (MODE. PLUS );
Break;
Case R. id. buttonMinus:
SetMode (MODE. MINUS );
Break;
Case R. id. buttonMultiply:
SetMode (MODE. MULTIPLY );
Break;
Case R. id. buttonDivide:
SetMode (MODE. DIVIDE );
Break;
Case R. id. buttonEval:
Calc ();
Break;
Case R. id. buttonDot:
AddNumber (".");
Break;
}
}
Private void calc (){
If (hadCalced & typeClean) return;
CurrentNum = new BigDecimal (currentText );
Try {
Switch (currentMode ){
Case PLUS:
LastNum = lastNum. add (currentNum );
Break;
Case MINUS:
LastNum = lastNum. subtract (currentNum );
Break;
Case MULTIPLY:
LastNum = lastNum. multiply (currentNum );
Break;
Case DIVIDE:
LastNum = lastNum. divide (currentNum, 16,
BigDecimal. ROUND_HALF_DOWN );
Break;
}
} Catch (Exception e ){
Toast. makeText (getApplicationContext (), "error:" + e. getMessage (), Toast. LENGTH_SHORT). show ();
Return;
}
String resultText = lastNum. toString ();
If (resultText. indexOf (".")> 0) {// removes the extra digits of a decimal number.
ResultText = resultText. replaceAll ("0 +? $ ","");
ResultText = resultText. replaceAll ("[.] $ ","");
}
SetResultText (resultText );
TypeClean = true;
HadCalced = false;
}
Private void setMode (MODE mode ){
If (! HadCalced &&! TypeClean ){
Calc ();
CurrentMode = mode;
Return;
}
LastNum = new BigDecimal (currentText );
CurrentMode = mode;
TypeClean = true;
HadCalced = true;
}

Private void addSign (){
If (currentText. equals ("0") | currentText. equals ("") return;
If (currentText. startsWith ("-") setResultText (currentText. substring (1 ));
Else setResultText ("-" + currentText );
}

Private void clear (){
CurrentNum = new BigDecimal ("0 ");
LastNum = new BigDecimal ("0 ");
TypeClean = false;
CurrentMode = MODE. PLUS;
SetResultText ("0 ");
}

Private void setResultText (String string ){
TextView. setText (string );
}

Public void addNumber (String num ){
String resultText = currentText;
If (typeClean = true ){
ResultText = "0 ";
TypeClean = false;
}
If (num. equals (".") & currentText. lastIndexOf (".")! =-1) return;
If (resultText. equals ("0") | resultText. equals (""))
&&! Num. equals ("."))
ResultText = "";
ResultText = resultText + num;
SetResultText (resultText );
}

Private void backSpace (){
If (currentText. equals ("0") | currentText. equals ("") return;
If (currentText. length () = 1 ){
SetResultText ("0 ");
Return;
}
SetResultText (currentText. substring (0, currentText. length ()-1 ));
}

@ Override
Public boolean onOptionsItemSelected (MenuItem item ){
If (item. getItemId () = R. id. action_about ){
New AlertDialog. builder (this ). setTitle ("about "). setMessage ("[Android www.111cn.net simple calculator] n author JerryLocken blog n source code/android-simple-calc "). setPositiveButton ("OK", null ). show ();
Return true;
}
Return super. onOptionsItemSelected (item );
}
}

From: http://www.111cn.net/sj/android/60746.htm


Android implements layout and calculator addition operations

Gridlayout is a good implementation, but this value supports Versions later than 4.0. There are a lot of examples on the Internet. You can check it out. Linearlayout can also be used, but it is complicated.
 
How does android use EditText to display all user input, such as 3 + 2-8 ??

StringBuffer sb = new StringBuffer ();
Sb. append ("3 ");
Sb. append ("+ ");
Sb. append ("2 ");
Sb. append ("-");
Sb. append ("8 ");
EditText. setText (sb. toString ());

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.