有谁知道为什么这段代码没有根据需要更改我的按钮的背景图像?
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(getApplicationContext(), "Size: " + Order.getInstance().getOrderItems().size(), Toast.LENGTH_SHORT).show();
price.setText(String.valueOf(Order.getInstance().getAmount()));
if(Order.getInstance().getOrderItems().size() > 0 && sent == false){
sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
sendBtn.setVisibility(View.VISIBLE);
sendBtn.setBackgroundResource(R.drawable.send);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Review Order Screen", Toast.LENGTH_SHORT).show();
Intent i = new Intent(v.getContext(), ReviewOrderActivity.class);
sent = true;
startActivity(i);
}
});
}else if(sent == true){
sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
sendBtn.setVisibility(View.VISIBLE);
sendBtn.setBackgroundResource(R.drawable.pay);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Pay Order Screen", Toast.LENGTH_SHORT).show();
//Intent i = new Intent(v.getContext(), PayActivity.class);
//startActivity(i);
}
});
}
}
如果用户第一次按下按钮,它应该将 a 设置Boolean
为 true 并启动新的 Activity。当用户按下按钮返回到上一个活动时,从新活动中,它现在应该将图像设置为不同,因为Boolean
设置为 true。
出于某种原因,它不起作用......非常感谢任何帮助!
这是 Activity 2 中的代码,它在按下按钮时将用户返回到上一个活动:
sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
sendBtn.setVisibility(View.VISIBLE);
sendBtn.setBackgroundResource(R.drawable.confirm);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent k = new Intent(v.getContext(), SectionsActivity.class);
int tableNum = Order.getInstance().getTableNumber();
int coverNum = Order.getInstance().getCoverNumber();
itemNames = new ArrayList<String>();
//Get dishes from order
for(int i = 0; i < orderItems.size(); i++){
MenuItem item = orderItems.get(i);
String itemName = item.getName();
itemNames.add(i, itemName);
}
RequestTask requestTask = new RequestTask(url, String.valueOf(tableNum), String.valueOf(coverNum), itemNames);
ReviewOrderActivity.this.finish();
//startActivity(k);
}
});
编辑代码:
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(getApplicationContext(), "value: " + sent, Toast.LENGTH_SHORT).show();
price.setText(String.valueOf(Order.getInstance().getAmount()));
if(Order.getInstance().getOrderItems().size() > 0 && sent == false){
sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
sendBtn.setVisibility(View.VISIBLE);
sendBtn.setBackgroundResource(R.drawable.send);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Review Order Screen " + sent, Toast.LENGTH_SHORT).show();
Intent i = new Intent(v.getContext(), ReviewOrderActivity.class);
startActivityForResult(i, 0);
}
});
}else if(sent == true){
sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
sendBtn.setVisibility(View.VISIBLE);
sendBtn.setBackgroundResource(R.drawable.pay);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Pay Order Screen", Toast.LENGTH_SHORT).show();
//Intent i = new Intent(v.getContext(), PayActivity.class);
//startActivity(i);
}
});
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
sent = true;
}
if (resultCode == RESULT_CANCELED) {
sent = false;
}
}
}//onActivityResult
在第二个活动中
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_review);
//Get order items from application and assign to list
orderItems = Order.getInstance().getOrderItems();
sendBtn = (ImageButton)findViewById(R.id.confirmOrder);
sendBtn.setVisibility(View.VISIBLE);
sendBtn.setBackgroundResource(R.drawable.confirm);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent k = new Intent(v.getContext(), SectionsActivity.class);
int tableNum = Order.getInstance().getTableNumber();
int coverNum = Order.getInstance().getCoverNumber();
itemNames = new ArrayList<String>();
//Get dishes from order
for(int i = 0; i < orderItems.size(); i++){
MenuItem item = orderItems.get(i);
String itemName = item.getName();
itemNames.add(i, itemName);
}
RequestTask requestTask = new RequestTask(url, String.valueOf(tableNum), String.valueOf(coverNum), itemNames);
setResult(RESULT_OK); //EDIT
ReviewOrderActivity.this.finish();
//startActivity(k);
}
});
//Create adapter and set to the list
adapter = new ReviewOrderArrayAdapter(this, orderItems);
this.setListAdapter(adapter);
}