不幸的是,我的所有字符串都不会出现在我创建的TextView
中。我想要两个TextViews;一个用于cost
,另一个用于time
。
MainActivity.java
public class MainActivity extends Activity {
int intTime = 30;
int intCost = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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));
}
});
}
}
ResultActivity.java
public class ResultActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
TextView time = (TextView)findViewById(R.id.txtTime);
TextView cost = (TextView)findViewById(R.id.txtCost);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String strTime = sharedPref.getString("keyTime", "");
String strCost = sharedPref.getString("keyTime", "");
cost.setText("cost" + (strCost));
time.setText("and " + strTime + " to deliver.");
}
}
有人有任何建议吗?我只需要在ResultActivity.java中的TextView
中显示strCost和strTime。
答案 0 :(得分:0)
确保在使用编辑器时进行提交:
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("keyTime", time);
editor.putString("keyCost", cost);
editior.commit();