When I was a beginner in Android, I wanted to create a calculator, but I used the following four symbols for selection. After my unremitting efforts, I finally realized this function.
Below I will post my code for your reference:
1. The main. xml file is used first. The Code is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Absolutelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Edittext
Android: Id = "@ + ID/edittext1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textsize = "18sp"
Android: layout_x = "5px"
Android: layout_y = "52px"
Android: textcolor = "@ color/color1"
/>
<Textview
Android: Id = "@ + ID/textview1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: singleline = "true"
Style = "? Android: ATTR/spinnerdropdownitemstyle"
/>
<Edittext
Android: Id = "@ + ID/edittext2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textsize = "18sp"
Android: layout_x = "200px"
Android: layout_y = "52px"
Android: textcolor = "@ color/color2"
/>
<Button
Android: Id = "@ + ID/button1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "computing"
Android: textsize = "25sp"
Android: layout_x = "270px"
Android: layout_y = "52px"
Android: textcolor = "@ color/color4"
/>
<Textview
Android: Id = "@ + ID/textview2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textsize = "25sp"
Android: layout_x = "12px"
Android: layout_y = "161px"
/>
<Spinner
Android: Id = "@ + ID/spinner1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textsize = "25sp"
Android: layout_x = "80px"
Android: layout_y = "52px"
/>
</Absolutelayout>
2. I created a new color. xml file and learned how to use the color. The Code is as follows: <? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Color name = "color1"> #00ff33 </color>
<Color name = "color2"> #00 aaff </color>
<Color name = "color3" >#456789 </color>
<Color name = "color4"> # dddd00 </color>
</Resources>
3. The original code of activity1.java is as follows: public class activity01 extends activity implements onclicklistener
{
Private Static final string [] jisuanfuhao = {"+ ","-","*","/"};
Private textview textview1, textview2;
Private edittext edittext1, edittext2;
Private button button1;
Private spinner spinner1;
Private arrayadapter adapter;
Animation myanimation;
@ Override
Public void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Edittext1 = (edittext) findviewbyid (R. Id. edittext1 );
Textview1 = (textview) findviewbyid (R. Id. textview1 );
Edittext2 = (edittext) findviewbyid (R. Id. edittext2 );
Button1 = (button) findviewbyid (R. Id. button1 );
Button1.setonclicklistener (this );
Textview2 = (textview) findviewbyid (R. Id. textview2 );
Spinner1 = (spinner) findviewbyid (R. Id. spinner1 );
Adapter = new arrayadapter (this, Android. R. layout. simple_spinner_item, jisuanfuhao );
Spinner1.setadapter (adapter );
Log. E ("", "Is this statement executed? ");
Spinner1.setonitemselectedlistener (new Spinner. onitemselectedlistener ()
{
@ Override
Public void onitemselected (adapterview <?> Arg0, view arg1, int arg2, long arg3)
{
Textview1.settext (jisuanfuhao [arg2]);
/* Display myspinner */
Arg0.setvisibility (view. Visible );
}
@ Override
Public void onnothingselected (adapterview <?> Arg0)
{
// Todo auto-generated method stub
}
});
}
@ Override
Public void onclick (view V)
{
Int firstnumber = integer. parseint (edittext1.gettext (). tostring ());
Int secondnumber = integer. parseint (edittext2.gettext (). tostring ());
String result = NULL;
If (textview1.gettext (). Equals ("+ "))
{
Result = string. valueof (firstnumber + secondnumber );
}
If (textview1.gettext (). Equals ("-"))
{
Result = string. valueof (firstnumber-secondnumber );
}
If (textview1.gettext (). Equals ("*"))
{
Result = string. valueof (firstnumber * secondnumber );
}
If (textview1.gettext (). Equals ("/"))
{
Result = string. valueof (firstnumber/secondnumber );
}
Textview2.settext (edittext1.gettext () + "" + textview1.gettext () + "" + edittext2.gettext () + "=" + result );
}
}