Draw the interface as
Add respective onClick events to each button.
And in Main Activity..
Declare following variables:
For operation button click:
For Clear Button:
and for the "=" button:
yeah, that's it and ur handy calculator is ready.
Add respective onClick events to each button.
And in Main Activity..
Declare following variables:
int vFirstNum=0,vSecondNum=0,vResult=0;and then for each digit button click:
int vTemp=0;
String vOperation="";
public void btn0_Click(View view){
EditText editText=(EditText)findViewById(R.id.editText1);
vTemp=Integer.parseInt(editText.getText().toString());
if(vTemp==0)
editText.setText("0");
else if(vTemp<=999999)
editText.setText(editText.getText()+"0");
}
For operation button click:
public void btnPlus_Click(View view){
vOperation="Add";
EditText editText=(EditText)findViewById(R.id.editText1);
vFirstNum=Integer.parseInt(editText.getText().toString());
editText.setText("0");
}
For Clear Button:
public void btnC_Click(View view){
EditText editText=(EditText)findViewById(R.id.editText1);
editText.setText("0");
}
and for the "=" button:
public void btnResult_Click(View view){
EditText editText=(EditText)findViewById(R.id.editText1);
vSecondNum=Integer.parseInt(editText.getText().toString());
if(vOperation=="Add")
vResult=vFirstNum+vSecondNum;
else if(vOperation=="Multiply")
vResult=vFirstNum*vSecondNum;
else if(vOperation=="Divide")
vResult=vFirstNum/vSecondNum;
else if(vOperation=="Minus")
vResult=vFirstNum-vSecondNum;
String vMessage=""+vResult;
editText.setText(vMessage);
vFirstNum=0;
vSecondNum=0;
vResult=0;
vOperation="";
}
yeah, that's it and ur handy calculator is ready.
No comments:
Post a Comment