In fact, this Android computer, all the backstage ideas and "C # calculator Code" is exactly the same. The Win form program is ported to Android, from C # to Java in fact, because the basic syntax is very similar, the only difficulty is the Android XML layout part, unlike C # forms can be directly dragged.
Or a calculator that can complete the basic arithmetic as follows:
Set the appropriate font for the button in Res\values\strings.xml to avoid the layout file warning flying:
<?xml version=" 1.0 "encoding=" Utf-8 "?> <resources> <string name=" App_n Ame "> Calculator </string> <string name=" bt_1 ">1</string> <string name=" bt_2 ">2</string> &L T;string name= "Bt_3" >3</string> <string name= "Bt_4" >4</string> <string name= "Bt_5" >5< /string> <string name= "bt_6" >6</string> <string name= "bt_7" >7</string> <string name= "b T_8 ">8</string> <string name=" Bt_9 ">9</string> <string name=" Bt_0 ">0</string> < String Name= "Bt_point" >.</string> <string name= "Bt_ce" >CE</string> <string name= "Bt_plus"
>+</string> <string name= "Bt_minus" >-</string> <string name= "Bt_multi" >x</string> <string name= "Bt_div" >÷</string> <string name= "Bt_result" >=</string> </resources>
Later, the layout section uses the "Android" about the percentage layout of multiple linearlayout nesting problems and solutions (click to open the link) of the idea, the following figure, a TextView, a edittext, are directly with Match_ Parent occupies the width of the whole line, then uses LinearLayout and tablelayout to divide the transverse proportions.
Therefore, the Res\layout\activity_main.xml specific code is as follows, after the operation to operate the component plus ID, here Plus "Android" Memory card picture reader, gallery app (Click on the link to open) The ScrollView is to prevent some phone screen too little, plus the vertical scroll bar:
<scrollview xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <linearlayout android:layout_width= "M Atch_parent "android:layout_height=" wrap_content "android:orientation=" vertical "> <textview andr Oid:id= "@+id/textview1" android:layout_width= "match_parent" android:layout_height= "wrap_content"/> &L T EditText android:id= "@+id/edittext1" android:layout_width= match_parent "android:layout_height=" Wrap_con Tent "android:enabled=" false "android:inputtype=" None "android:textsize=" 18sp "/> <linearlayo UT android:baselinealigned= "false" Android:layout_width= "Match_parent" android:layout_height= "Match_pare" NT "> <tablelayout android:layout_width=" 0dp "android:layout_height=" Match_parent "an droid:layout_weight= "2" >
<linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" &G
T <button android:id= "@+id/bt_7" android:layout_width= "0DP" android:layout_height= "W
Rap_content "android:layout_weight=" 1 "android:text=" @string/bt_7 "/> <button
Android:id= "@+id/bt_8" android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "@string/bt_8"/> <button Andro Id:id= "@+id/bt_9" android:layout_width= "0DP" android:layout_height= "Wrap_content" and roid:layout_weight= "1" android:text= "@string/bt_9"/> </LinearLayout> <linearlayo
UT android:layout_width= "match_parent" android:layout_height= "wrap_content" > <button Android:id= "@+id/bt_4" android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "@string/bt_4"/> <button android:id= "@+id/bt_5" android:layout_width= "0DP" android:layout_height= "Wrap_content" android:l
ayout_weight= "1" android:text= "@string/bt_5"/> <button android:id= "@+id/bt_6" Android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1 "Android:text=" @string/bt_6 "/> </LinearLayout> <linearlayout android: Layout_width= "Match_parent" android:layout_height= "Wrap_content" > <button android: Id= "@+id/bt_1" android:layout_width= "0DP" android:layout_height= "Wrap_content" Android
: layout_weight= "1" android:text= "@string/bt_1"/> <button android:id= "@+id/bt_2" Android:layo Ut_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:
text= "@string/bt_2"/> <button android:id= "@+id/bt_3" android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "@string/b
T_3 "/> </LinearLayout> <linearlayout android:layout_width=" Match_parent " android:layout_height= "Wrap_content" > <button android:id= "@+id/bt_0" Android:la Yout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" Androi d:text= "@string/bt_0"/> <button android:id= "@+id/bt_point" android:layout_width= "0DP" Android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "@string/bt_point"/> </LinearLayout> </TableLayout> <tablelayout android:layout_width= "0DP" an droid:layout_height= "Match_parent" android:layout_weight= "1" > <button android:id= "@+id/b" T_ce "android:layout_width=" match_parent "android:layout_height=" Wrap_content "android:text=" "@string/bt_ce"/> <linearlayout android:layout_width= "Match_parent" Android:layout_hei
ght= "Wrap_content" > <button android:id= "@+id/bt_plus" android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "@string
Bt_plus "/> <button android:id=" @+id/bt_minus "android:layout_width=" 0DP " Android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "@string/bt_minus"/> </lin earlayout> <linearlayout android:layout_width= "match_parent" android:layout_height= "Wra"
P_content "> <button android:id=" @+id/bt_multi "android:layout_width=" 0DP " android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "@string/bt_mult I "/> <button android:id=" @+id/bt_div "android:layout_width=" 0DP "and
roid:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "@string/bt_div"/> </LinearLayout> <button android:id= "@+id/bt_result" android:layout_width= "MATC H_parent "android:layout_height=" wrap_content "android:text=" @string/bt_result "/> </table Layout> </lineArlayout> </LinearLayout> </ScrollView>
After is Mainactivity.java nothing to say, the basic and direct win form, "C # Calculator code," to convert C # to Java is a very simple thing. The only thing to note is that there are more buttons here, so it is not recommended to use the internal anonymous class to implement the click of the button, like "Android" using the Java Code Layout, button add Click event (click Open link), You should let mainactivity implement the Onclicklistener interface, then in the inherited OnClick method, based on the pass over the ID in view V, using the switch-case structure to engage, so clear.
Package com.calculator;
Import java.util.*;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.EditText;
Import Android.widget.TextView;
Import android.app.Activity; public class Mainactivity extends activity implements Onclicklistener {private list<double> value_list = new Arra Ylist<double> ()//The number entered by the user private list<integer> operator_list = new arraylist<integer> ();
The operator entered by the user, defined + is 0,-as 1,x for 2,÷ 3//Status record private Boolean Add_flag = false;//+ Press Private Boolean Minus_flag = false;//-Press Private Boolean multi_flag = false;//x Press Private Boolean div_flag = False;//÷ Press Private Boolean result_flag = false;//
= Press Private Boolean Can_operate_flag = false;//pressed = Whether to respond to private TextView textView1;
Private EditText EditText1;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main); Findviewbyid (R.ID.BT_0).Setonclicklistener (this);
Findviewbyid (r.id.bt_1). Setonclicklistener (this);
Findviewbyid (r.id.bt_2). Setonclicklistener (this);
Findviewbyid (r.id.bt_3). Setonclicklistener (this);
Findviewbyid (R.id.bt_4). Setonclicklistener (this);
Findviewbyid (r.id.bt_5). Setonclicklistener (this);
Findviewbyid (r.id.bt_6). Setonclicklistener (this);
Findviewbyid (r.id.bt_7). Setonclicklistener (this);
Findviewbyid (r.id.bt_8). Setonclicklistener (this);
Findviewbyid (R.id.bt_9). Setonclicklistener (this);
Findviewbyid (R.id.bt_point). Setonclicklistener (this);
Findviewbyid (R.ID.BT_CE). Setonclicklistener (this);
Findviewbyid (R.id.bt_plus). Setonclicklistener (this);
Findviewbyid (R.id.bt_minus). Setonclicklistener (this);
Findviewbyid (R.id.bt_multi). Setonclicklistener (this);
Findviewbyid (R.id.bt_div). Setonclicklistener (this);
Findviewbyid (R.id.bt_result). Setonclicklistener (this);
TextView1 = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
EditText1 = (edittext) Findviewbyid (R.ID.EDITTEXT1); } @Override Public VoiD OnClick (View v) {switch (V.getid ()) {case R.id.bt_0:num_down ("0");
Break
Case R.id.bt_1:num_down ("1");
Break
Case R.id.bt_2:num_down ("2");
Break
Case R.id.bt_3:num_down ("3");
Break
Case R.id.bt_4:num_down ("4");
Break
Case R.id.bt_5:num_down ("5");
Break
Case R.id.bt_6:num_down ("6");
Break
Case R.id.bt_7:num_down ("7");
Break
Case R.id.bt_8:num_down ("8");
Break
Case R.id.bt_9:num_down ("9");
Break
Case R.id.bt_point:num_down (".");
Break
Case R.id.bt_plus:if (!add_flag)//Prevents users from entering a symbol key multiple times, the symbol key allows only one entry {Result_flag = false; Value_list.add (Double.parsedouble (Edittext1.gettext (). toString ());//Put the currently entered number into Value_list operator_list.add (0)
;
Textview1.settext (Textview1.gettext () + "+");
Add_flag = true;
Can_operate_flag = false;//just finished entering the symbol, can not constitute a normal expression, such as 111+, set to not run state} break;
Case R.id.bt_minus:if (!minus_flag) {Result_flag = false;
Value_list.add (double.parsedouble) (Edittext1.gettext () . toString ()));
Operator_list.add (1);
Textview1.settext (Textview1.gettext () + "-");
Minus_flag = true;
Can_operate_flag = false;
} break;
Case R.id.bt_multi:if (!multi_flag) {Result_flag = false;
Value_list.add (Double.parsedouble (Edittext1.gettext (). toString ()));
Operator_list.add (2); Textview1.settext ("(" + textview1.gettext () + ") x");//Add parentheses to the previous item that has been entered.
(operator stack problem is a very complex data structure problem, not done here: P) Multi_flag = true;
Can_operate_flag = false;
} break;
Case R.id.bt_div:if (!div_flag) {Result_flag = false;
Value_list.add (Double.parsedouble (Edittext1.gettext (). toString ()));
Operator_list.add (3);
Textview1.settext ("(" + textview1.gettext () + ")");
Div_flag = true;
Can_operate_flag = false;
} break; Case R.id.bt_result:if (value_list.size () > 0 && operator_list.size () > 0 && can_operate_flag)
{//need to prevent users from not entering a number, or just enter a number, click =.
Value_list.add (Double.parsedouble (Edittext1.gettext (). toString ())); Double total =Value_list.get (0); for (int i = 0; i < operator_list.size (); i++) {int _operator = Operator_list.get (i);//operator is a keyword for C # operator overloading, preceded by _
To distinguish switch (_operator) {case 0:total + + value_list.get (i + 1);
Break
Case 1:total-= value_list.get (i + 1);
Break
Case 2:total *= Value_list.get (i + 1);
Break
Case 3:total/= Value_list.get (i + 1);
Break
} edittext1.settext (Total + "");
Textview1.settext (Total + "");
Operator_list.clear () and/or finish, empty the cumulative number and operation array value_list.clear ();
Result_flag = true;//= Press} break;
Case R.id.bt_ce:operator_list.clear ();
Value_list.clear ();
Add_flag = false;
Minus_flag = false;
Multi_flag = false;
Div_flag = false;
Result_flag = false;
Can_operate_flag = false;
Edittext1.settext ("");
Textview1.settext ("");
Break }//Number key pressed, containing 0 and., similar to 000001223 such cases are allowed here, because Java can speak 000001223 itself into 1223 private void Num_down (String num) {if Add_flag | | Minus_flag | | Multi_flag | | Div_flag | | Result_flag) {if (Result_flag)//By the equal sign, just finished the state of an operation {Textview1.settext ("");
Edittext1.settext ("");//If the user has just entered an operator Add_flag = false;
Minus_flag = false;
Multi_flag = false;
Div_flag = false;
Result_flag = false; if (Num.equals (".") && Edittext1.gettext (). toString (). IndexOf (".") < 0) | |!num.equals (".")
{//If the user has entered a decimal point, determine if the currently entered number contains a decimal. Allow input Edittext1.settext (edittext1.gettext () + num);
Textview1.settext (Textview1.gettext () + num);
Can_operate_flag = true;
}
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.