android计算器代码示例分享
<?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;
}
}
}
}
上一篇: 红枸杞和黑枸杞的区别你知道几个
下一篇: C#中Path类的常用方法