我在java中有一个问题。
package apa.y;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
public class myprice extends Activity {
/** Called when the activity is first created. */
CheckBox book,pencil;
TextView total;
int sum=0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mymain);
pencil = (CheckBox) findViewById(R.id.pencil);
book = (CheckBox) findViewById(R.id.book);
total = (TextView) findViewById(R.id.total);
}
public void onClick(View v) {
sum = 0;
if (pencil.isChecked()) {sum += 10;}else{sum +=0;}
if (book.isChecked()) {sum += 5;}else{sum +=0;}
total.setText("Total: "+sum);
}
}
如果书和铅笔是复选框和 1 个文本视图,如何求和?我现有的代码不起作用。