按钮背景不会根据需要改变

时间:2013-03-16 00:19:36

标签: android button imagebutton

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

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

}

如果用户第一次按下按钮,则应将Boolean设置为true并启动新活动。当用户按下按钮返回上一个活动时,在新活动中,现在应该将图像设置为不同,因为Boolean设置为true。

由于某种原因,它不起作用......非常感谢任何帮助!

这是活动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);
}

1 个答案:

答案 0 :(得分:2)

由于无法保证Android不会清除您的variables甚至是您的Activity,如果它需要内存或GC无法获取内存,您需要处理在variables中存储这些onPause()。您可以将它们存储在SharedPrefs中,以便在onResume()

中检索它们

您需要的另一个可能更简单的选项是使用StartActivityForResult()setResult() SecondActivity中的RESULT_CODE传回variable表示更改trueFirstActivity。然后,您无需在REQUEST_CODE

中进行设置

来自Docs

  

requestCode如果&gt; = 0,当活动退出时,将在onActivityResult()中返回此代码。

您应该查找您发送的startActivityForResult。这样,如果您需要不同的内容,可以在同一个Activity中多次使用{{1}}