0

有谁知道为什么这段代码没有根据需要更改我的按钮的背景图像?

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);
}
4

1 回答 1

2

由于无法保证 Android 不会清除您variables甚至您Activity的内存(如果它需要内存)或者 GC 不会占用它,因此您需要variablesonPause(). 您可以将它们存储在其中,SharedPrefs以便您可以在其中检索它们onResume()

您需要的另一个可能更简单的选项是使用StartActivityForResult()和 insetResult()SecondActivity将传回一个RESULT_CODE指示以将其更改variabletrue. 然后你就不需要在你的FirstActivity

文档

requestCode 如果 >= 0,则活动退出时会在 onActivityResult() 中返回此代码。

你应该寻找REQUEST_CODE你发送的。这样,如果您需要不同的东西,您可以startActivityForResult多次使用Activity

于 2013-03-16T00:33:46.673 回答