这是我希望你通过的代码。请指出我的错误。这是一个计算器应用程序的简单代码。我不确定,但错误可能在代码的按钮部分。如果您在这方面帮助我,将不胜感激。在此先感谢您。
package com.iitg.sau_calc;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Cal extends Activity {
Button one, two, thr, fou, fiv, six, sev, eig, nin, zer, add, sub, mul, div, clr, equ;
TextView scr;
int ans=0,fans=0,ca=0,cs=0,cm=0,cd=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cal);
one=(Button)findViewById(R.id.one);
two=(Button)findViewById(R.id.two);
thr=(Button)findViewById(R.id.thr);
fou=(Button)findViewById(R.id.fou);
fiv=(Button)findViewById(R.id.fiv);
six=(Button)findViewById(R.id.six);
sev=(Button)findViewById(R.id.sev);
eig=(Button)findViewById(R.id.eig);
nin=(Button)findViewById(R.id.nin);
zer=(Button)findViewById(R.id.zer);
add=(Button)findViewById(R.id.add);
sub=(Button)findViewById(R.id.sub);
mul=(Button)findViewById(R.id.mul);
div=(Button)findViewById(R.id.div);
clr=(Button)findViewById(R.id.clr);
equ=(Button)findViewById(R.id.equ);
scr=(TextView)findViewById(R.id.Screen);
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+1;
scr.setText(ans);
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+2;
scr.setText(ans);
}
});
thr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+3;
scr.setText(ans);
}
});
fou.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+4;
scr.setText(ans);
}
});
fiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+5;
scr.setText(ans);
}
});
six.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+6;
scr.setText(ans);
}
});
sev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+7;
scr.setText(ans);
}
});
eig.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+8;
scr.setText(ans);
}
});
nin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+9;
scr.setText(ans);
}
});
zer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10;
scr.setText(ans);
}
});
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Ans();
ca+=1;
scr.setText("+");
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Ans();
cs+=1;
scr.setText("-");
}
});
mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Ans();
cm+=1;
scr.setText("*");
}
});
div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Ans();
cd+=1;
scr.setText("/");
}
});
equ.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Ans();
scr.setText(fans);
}
});
clr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=0;
fans=0;
ca=0;cm=0;cd=0;cs=0;
scr.setText("0");
}
});
}
public void Ans(){
if(ca>0)
{
fans=fans+ans;
ca=0;
}
else if(cs>0)
{
fans=fans-ans;
cs=0;
}
else if(cm>0)
{
fans=fans*ans;
cm=0;
}
else if(cd>0)
{
fans=fans/ans;
cd=0;
}
else
{
fans=ans;
ans=0;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.cal, menu);
return true;
}
}