希望能够在最后创建一个具有Time和Cost值的变量,我将最终在TextView中显示。尽管我遇到了一些问题。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int intTime = 30;
final int intCost = 10;
CheckBox CP = (CheckBox)findViewById(R.id.checkPepperoni);
CheckBox CS = (CheckBox)findViewById(R.id.checkSausage);
CheckBox CB = (CheckBox)findViewById(R.id.checkBacon);
CheckBox CM = (CheckBox)findViewById(R.id.checkMushroom);
final ImageView cheese =(ImageView)findViewById(R.id.imgCheese);
final ImageView pepperoni =(ImageView)findViewById(R.id.imgPepperoni);
final ImageView sausage =(ImageView)findViewById(R.id.imgSausage);
final ImageView bacon =(ImageView)findViewById(R.id.imgBacon);
final ImageView mushroom =(ImageView)findViewById(R.id.imgMushroom);
final SharedPreferences sharedPref =PreferenceManager.getDefaultSharedPreferences(this);
CP.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
//Pepperoni Listener
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked1) {
// TODO Auto-generated method stub
if(isChecked1){
pepperoni.setImageResource(R.drawable.pepperoni);
intTime = intTime + 6;
intCost = intCost + 5;
}
else{
pepperoni.setImageResource(0);
intTime = intTime - 6;
intCost = intCost - 5;
}
}});
CS.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
//Sausage Listener
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked2) {
// TODO Auto-generated method stub
if(isChecked2){
sausage.setImageResource(R.drawable.sausage);
intTime = intTime + 6;
intCost = intCost + 5;
}
else{
sausage.setImageResource(0);
intTime = intTime - 6;
intCost = intCost - 5;
}
}});
CB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked3) {
// TODO Auto-generated method stub
if(isChecked3){
bacon.setImageResource(R.drawable.bacon);
intTime = intTime + 6;
intCost = intCost + 5;
}
else{
bacon.setImageResource(0);
intTime = intTime - 6;
intCost = intCost - 5;
}
}});
CM.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked4) {
// TODO Auto-generated method stub
if(isChecked4){
mushroom.setImageResource(R.drawable.mushroom);
intTime = intTime + 6;
intCost = intCost + 5;
}
else{
mushroom.setImageResource(0);
intTime = intTime - 6;
intCost = intCost - 5;
}
}});
ImageButton IB = (ImageButton)findViewById(R.id.imgBtnGo);
IB.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String time = Integer.toString(intTime);
String cost = Integer.toString(intCost);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("keyTime", time);
editor.putString("keyCost", cost);
startActivity(new Intent(MainActivity.this, ResultActivity.class));
}
});
}
}
我遇到的错误是“无法分配最终的局部变量intCost,因为它是在封闭类型中定义的。”
这意味着什么,是否有任何方法可以使用所有复选框更改此变量?
答案 0 :(得分:1)
问题的原因是final
变量应在声明时初始化,无法重新分配。对于这种情况,最好的选择是在匿名类中创建以最终变量值开头的内部字段,并且这些内部字段会定期更新。
示例:
CP.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//Pepperoni Listener
int innerTime = intTime;
int innerCost = intCost;
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked1) {
if(isChecked1) {
pepperoni.setImageResource(R.drawable.pepperoni);
innerTime += 6;
innerCost += 5;
} else {
pepperoni.setImageResource(0);
innerTime -= 6;
innerCost -= 5;
}
}
});
更多信息:
答案 1 :(得分:0)
我不能为什么你首先使用midficator final
,如果你删除它,你的代码将正常工作,我不确定,但我已检查onClick
和这是工作。只要让他们成为领域,就没事了