Java 安卓没有';我似乎没有正确地计算
基本上,有一个文本字段,用户可以在其中输入膳食小计。然后,当他们点击done时,done方法应该获取tip字段值,并将其乘以小计值,这样总数就包括了这两条数据。但是,当我点击“完成”按钮时,结果实际上总是等于小计,并且提示总是被忽略。代码如下:Java 安卓没有';我似乎没有正确地计算,java,android,math,Java,Android,Math,基本上,有一个文本字段,用户可以在其中输入膳食小计。然后,当他们点击done时,done方法应该获取tip字段值,并将其乘以小计值,这样总数就包括了这两条数据。但是,当我点击“完成”按钮时,结果实际上总是等于小计,并且提示总是被忽略。代码如下: public class MainActivity extends Activity implements OnRatingBarChangeListener { // Testing Stuff to show the rating valu
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+"");
}
}
您从未将
OnClickListener
放在按钮变量done
上,因此您的单击被忽略,这就是该值不变的原因。您应该添加侦听器,然后在onClick()
中调用done()
方法。您还应该删除done()
的View视图
参数,因为您根本不使用它
// ...
done = (Button) findViewById(R.id.buttonDone);
// add listener
done.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
done();
}
});
// ...
您可能应该将方法的名称
done()
更改为displayResult()
,或者更改为更有意义且与名为done
的按钮变量不同的名称。实际上,您可能应该将大多数变量的名称更改为更清楚地描述变量用途的名称。例如,表示按钮的变量done
可以类似于btnDone
。您已将totalCost
声明为int
。因此,如果(小计成本*提示)
小于1,则总成本=小计成本
。如果适用,尝试使用浮动
而不是整数
。不要将整个代码粘贴在此处,只显示问题的相关部分,这样更容易发现错误Roid确实“正确”地进行了计算。因此,这是代码中的一个bug——使用调试器观察输入、状态和输出,以明确“不工作”的含义,并隔离最小的测试用例。(同样,使用的IDE(例如Eclipse)与观察到的结果无关。)什么?我一点也不明白。XML中的onClick方法应该可以工作。。。