欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android计算器代码示例分享

程序员文章站 2023-11-14 08:29:22
复制代码 代码如下:

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<edittext
    android:id="@+id/edittext1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >

    <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="=" />

复制代码 代码如下:

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;
        }
    }
}
}