android简单计算器代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
package hua.xing.demo;
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 CalculatordemoActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */
private Button but0 = null;
private Button but1 = null;
private Button but2 = null;
private Button but3 = null;
private Button but4 = null;
private Button but5 = null;
private Button but6 = null;
private Button but7 = null;
private Button but8 = null;
private Button but9 = null;
private Button dim = null;
private Button add = null;
private Button sub = null;
private Button mult = null;
private Button div = null;
private Button equ = null;
private EditText etext = null;
private double value = 0;
private String str1 = null;
private String tem = "";
private String sign = "a";
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
super.setContentView(yout.main);
add = (Button) findViewById(R.id.add);
sub = (Button) findViewById(R.id.sub);
mult = (Button) findViewById(R.id.mult);
div = (Button) findViewById(R.id.divid);
equ = (Button) findViewById(R.id.caculate);
but1 = (Button) findViewById(R.id.but1);
but3 = (Button) findViewById(R.id.but3);
but2 = (Button) findViewById(R.id.but2);
but4 = (Button) findViewById(R.id.but4);
but5 = (Button) findViewById(R.id.but5);
but6 = (Button) findViewById(R.id.but6);
but7 = (Button) findViewById(R.id.but7);
but8 = (Button) findViewById(R.id.but8);
but9 = (Button) findViewById(R.id.but9);
but0 = (Button) findViewById(R.id.but0);
dim = (Button) findViewById(R.id.dim);
etext = (EditText) findViewById(R.id.etext);
add.setOnClickListener(new AListener());
sub.setOnClickListener(new AListener());
mult.setOnClickListener(new AListener());
div.setOnClickListener(new AListener());
equ.setOnClickListener(new Listener());
but1.setOnClickListener(this);
but3.setOnClickListener(this);
but2.setOnClickListener(this);
but4.setOnClickListener(this);
but5.setOnClickListener(this);
but6.setOnClickListener(this);
but7.setOnClickListener(this);
but8.setOnClickListener(this);
but9.setOnClickListener(this);
but0.setOnClickListener(this);
dim.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.equals(but1)) {
tem = tem + "1";
etext.setText(tem);
}
if (v.equals(but2)) {
tem = tem + "2";
etext.setText(tem);
}
if (v.equals(but3)) {
tem = tem + "3";
etext.setText(tem);