基本上,有一个文本字段,用户可以输入用餐的小计。然后,当它们完成时,done方法应该取最终字段值,并将其乘以小计值,以便总计包含这两个数据。但是,当我点击完成按钮时,结果实际上总是等于小计,并且小费始终被忽略。下面是代码:Android似乎没有正确计算
public class MainActivity extends Activity implements OnRatingBarChangeListener {
// Testing Stuff to show the rating value, will need to use later for maths
static RatingBar rb;
TextView tipsTV;
ImageView greyPlus, greyMinus, greyPlus2, greyMinus2;
TextView peopleDiningTV, peopleDiningTitle;
int peopleDining = 2;
TextView tipValue;
int tipValueInt = 10;
TextView subtotal, total;
TextView subtotalTitle, totalTitle;
TextView epp, eppTitle;
Button done;
// Elements for hiding and such
static RelativeLayout rl;
static Button settingsButton;
public static int rating = 3;
// The Image used as the DropDown button, Rotate code below
ImageView dropDownButton;
Boolean hasRotated = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dropDownButton = (ImageView) findViewById(R.id.dropDownButton);
rb = (RatingBar) findViewById(R.id.ratingBar1);
rb.setRating(rating);
tipsTV = (TextView) findViewById(R.id.textView2);
tipValue = (TextView) findViewById(R.id.tipText);
greyPlus = (ImageView) findViewById(R.id.greyPlus);
greyMinus = (ImageView) findViewById(R.id.greyMinus);
greyPlus2 = (ImageView) findViewById(R.id.greyPlus2);
greyMinus2 = (ImageView) findViewById(R.id.greyMinus2);
peopleDiningTV = (TextView) findViewById(R.id.textViewPeople);
peopleDiningTitle = (TextView) findViewById(R.id.TextView02);
subtotal =(TextView) findViewById(R.id.subtotalText);
subtotalTitle =(TextView) findViewById(R.id.subtotalTitle);
total =(TextView) findViewById(R.id.totalText);
totalTitle =(TextView) findViewById(R.id.totalTitle);
epp = (TextView) findViewById(R.id.eppText);
eppTitle = (TextView) findViewById(R.id.eppTitle);
done = (Button) findViewById(R.id.buttonDone);
greyPlus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tipValueInt++;
tipValue.setText(tipValueInt + "%");
}
});
greyMinus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (tipValueInt >= 1) {
tipValueInt--;
tipValue.setText(tipValueInt + "%");
}
if(tipValueInt == 0){
tipValue.setText("No Tip.");
}
}
});
greyPlus2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
peopleDining++;
peopleDiningTV.setText(peopleDining + "");
}
});
greyMinus2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (peopleDining > 1) {
peopleDining--;
peopleDiningTV.setText(peopleDining + "");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
FragmentManager fm = getFragmentManager();
QuizFragment qf = new QuizFragment();
public void dropDown(View view) {
if (hasRotated == false) {
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out);
dropDownButton.setRotation(90);
ft.add(R.id.quizFragment, qf);
ft.show(qf);
ft.commit();
hasRotated = true;
// Hiding Elements, so they don't show through the fragment
tipsTV.setVisibility(View.INVISIBLE);
greyPlus.setVisibility(View.INVISIBLE);
greyMinus.setVisibility(View.INVISIBLE);
tipValue.setVisibility(View.INVISIBLE);
greyPlus2.setVisibility(View.INVISIBLE);
greyMinus2.setVisibility(View.INVISIBLE);
peopleDiningTV.setVisibility(View.INVISIBLE);
peopleDiningTitle.setVisibility(View.INVISIBLE);
subtotal.setVisibility(View.INVISIBLE);
subtotalTitle.setVisibility(View.INVISIBLE);
total.setVisibility(View.INVISIBLE);
totalTitle.setVisibility(View.INVISIBLE);
epp.setVisibility(View.INVISIBLE);
eppTitle.setVisibility(View.INVISIBLE);
done.setVisibility(View.INVISIBLE);
} else if (hasRotated == true) {
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_out,
android.R.animator.fade_out);
dropDownButton.setRotation(0);
hasRotated = false;
ft.remove(qf);
ft.commit();
// Hiding Elements, so they don't show through the fragment
tipsTV.setVisibility(View.VISIBLE);
greyPlus.setVisibility(View.VISIBLE);
greyMinus.setVisibility(View.VISIBLE);
tipValue.setVisibility(View.VISIBLE);
greyPlus2.setVisibility(View.VISIBLE);
greyMinus2.setVisibility(View.VISIBLE);
peopleDiningTV.setVisibility(View.VISIBLE);
peopleDiningTitle.setVisibility(View.VISIBLE);
subtotal.setVisibility(View.VISIBLE);
subtotalTitle.setVisibility(View.VISIBLE);
total.setVisibility(View.VISIBLE);
totalTitle.setVisibility(View.VISIBLE);
epp.setVisibility(View.VISIBLE);
eppTitle.setVisibility(View.VISIBLE);
done.setVisibility(View.VISIBLE);
}
}
public void openSettings(View view) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
}
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromTouch) {
}
public void done(View view){
int subtotalCost = Integer.parseInt(subtotal.getText().toString());
int tip = tipValueInt/100;
int totalCost = (subtotalCost * tip) + subtotalCost;
total.setText(totalCost+"");
}
}
不要在这里粘贴整个代码,只显示您的问题的相关部分,这使得更容易发现错误 – nem035 2014-10-03 23:13:01
Android *确实*做数学“正确”。因此,它是代码中的一个错误 - 使用调试器观察* input *,* state *和* output *来清除“不工作”意味着什么并隔离最小测试用例。 (也使用IDE - 例如Eclipse - 与观察结果无关。) – user2864740 2014-10-03 23:43:11