Android Calculator code sample sharing _android

Source: Internet
Author: User

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<edittext
Android:id= "@+id/edittext1"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:ems= "Ten" >

<requestfocus/>
</EditText>

<button
Android:id= "@+id/button1"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "1"/>

<button
Android:id= "@+id/button2"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "2"/>

<button
Android:id= "@+id/button3"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "+"/>

<button
Android:id= "@+id/button4"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "="/>

Copy Code code as follows:

Package com.action;

Import Android. R.integer;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;

public class Jspactivity extends activity{
Private Button bt1;
Private Button BT2;
Private Button Btadd;
Private Button BTEQL;

Private EditText et;

private int A;
private int B;

Private String stra= "";
Private String FH;
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.jsp);
bt1= (Button) Findviewbyid (R.id.button1);
Bt2= (Button) Findviewbyid (R.id.button2);
Btadd= (Button) Findviewbyid (R.id.button3);
Bteql= (Button) Findviewbyid (R.ID.BUTTON4);

    et= (edittext) Findviewbyid (R.ID.EDITTEXT1);
    Bt1.setonclicklistener (new sum ());
    Bt2.setonclicklistener (new sum ());
    Btadd.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
A=integer.parseint (Et.gettext (). toString ());
Et.settext ("");
Stra= "";
Fh=btadd.gettext (). toString ();
}
});
Bteql.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
B=integer.parseint (Et.gettext (). toString ());
Et.settext ("");
Stra= "";
if (fh.equals ("+"))
{
int c=a+b;
Et.settext ("" +c);
}
}
});
}
public class sum implements onclicklistener{

    @Override
    public void OnClick (View v) {
        //TODO auto-generated method stub
        switch (V.getid () {
        case r.id.button1:
             Stra+=bt1.gettext ();
            Et.settext (stra);
            break;
        case R.id.button2:
             Stra+=bt2.gettext ();
            Et.settext (stra);
            break;

Default
Break
}
}
}
}

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.