#ffc0cb〈/color>
#c0c0c0
3、java代码:
各种组件的使用在代码里都有明确说明,在此不再说明.
packagecom.example。
calculator;
importandroid.os.Bundle;
importandroid。
app。
Activity;
importandroid。
view.Menu;
importandroid。
view。
View;
importandroid。
view.View.OnClickListener;
importandroid。
widget。
Button;
importandroid.widget。
EditText;
importandroid.widget。
TextView;
publicclassMyCalculatorextendsActivity{
doubleop1=0,op2=0;//记录两次输入的值
charoperation;//记录运算符
EditTextinput;//输入文本框
booleanisInputChar=false;//判断是否输入了运算符
TextViewmemory;//记忆文本
Buttonplus,minus,multi,divide,equal;//运算符
Buttonback,clean,cleanAll;//文本数据处理(回退、本次输
//入清零、所有输入清零)
Buttonmclean,mread,msave;//数据记忆(清除、读取、保存)
Button[]btn=newButton[10];//数字
Buttondot;//点
Buttonsign;//正负数转换
@Override
publicvoidonCreate(BundlesavedInstanceState){
super。
onCreate(savedInstanceState);
setContentView(R。
layout.activity_my_calculator);
input=(EditText)findViewById(R。
id。
et_input);
memory=(TextView)findViewById(R。
id.tv_mem);
//运算符
plus=(Button)findViewById(R。
id.button_plus);
minus=(Button)findViewById(R.id.button_minus);
multi=(Button)findViewById(R。
id。
button_multi);
divide=(Button)findViewById(R.id.button_div);
equal=(Button)findViewById(R。
id。
button_equal);
//设置点击监听
plus。
setOnClickListener(listener);
minus.setOnClickListener(listener);
multi。
setOnClickListener(listener);
divide。
setOnClickListener(listener);
equal。
setOnClickListener(listener);
//记忆
back=(Button)findViewById(R。
id。
button_bk);
clean=(Button)findViewById(R.id。
button_ce);
cleanAll=(Button)findViewById(R。
id。
button_c);
back。
setOnClickListener(listener);
clean。
setOnClickListener(listener);
cleanAll。
setOnClickListener(listener);
//数据记忆
mclean=(Button)findViewById(R。
id。
button_mc);
mread=(Button)findViewById(R。
id.button_mr);
msave=(Button)findViewById(R.id。
button_ms);
mclean.setOnClickListener(listener);
mread.setOnClickListener(listener);
msave.setOnClickListener(listener);
//数字
btn[0]=(Button)findViewById(R。
id。
button_0);
btn[1]=(Button)findViewById(R。
id。
button_1);
btn[2]=(Button)findViewById(R.id。
button_2);
btn[3]=(Button)findViewById(R。
id。
button_3);
btn[4]=(Button)findViewById(R。
id.button_4);
btn[5]=(Button)findViewById(R。
id。
button_5);
btn[6]=(Button)findViewById(R。
id.button_6);
btn[7]=(Button)findViewById(R。
id。
button_7);
btn[8]=(Button)findViewById(R。
id。
button_8);
btn[9]=(Button)findViewById(R。
id。
button_9);
for(inti=0;i<10;i++){
btn[i].setOnClickListener(listener);
}
//点
dot=(Button)findViewById(R。
id。
button_dot);
dot。
setOnClickListener(listener);
//正负数转换
sign=(Button)findViewById(R.id。
button_sign);
sign.setOnClickListener(listener);
}
//按钮监听
privateOnClickListenerlistener=newOnClickListener(){
publicvoidonClick(Viewv){
//TODOAuto—generatedmethodstub
//获取输入数据
StringinputText=input.getText().toString();
//对输入数据进行判断
switch(v。
getId()){
caseR。
id。
button_bk:
//后退
if(inputText。
length()>1)
input。
setText(inputText。
substring(0,inputText。
length()—1));
elseif(inputText.length()==1){
input.setText(”0”);
}
if(input.getText().toString()。
compareTo("-")==0){//去除负号
input。
setText("0”);
}
break;
caseR.id。
button_ce:
//此次输入清零
input。
setText(”0”);
if(isInputChar)
op2=0;
else
op1=0;
break;
caseR.id.button_c:
//全部清零,重新计算
input.setText(”0”);
op1=0;
op2=0;
isInputChar=false;
break;
caseR.id。
button_mc:
//清空记忆数据
memory.setText("0”);