如何将我的信息连接到我的Toast消息

时间:2013-10-04 02:55:35

标签: java android eclipse

package edu.apsu.assignment4;



import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.widget.RadioGroup;
import android.widget.CheckBox;
import android.widget.TextView;


public class MainActivity extends Activity implements
android.view.View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    Button sendButton,aboutButton;
    EditText DrinkNum;
    Double subtotal;
    TextView order,aboutApp,Sroom,drinkType,Croom,go,flavor;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RadioGroup drink = (RadioGroup) findViewById(R.id.drinks);
        drink.setOnClickListener(this);

        CheckBox Croom = (CheckBox) findViewById(R.id.cream);
        Croom.setOnCheckedChangeListener(this);

        DrinkNum=(EditText) findViewById(R.id.editText1);

        sendButton=(Button) findViewById(R.id.send);
        sendButton.setOnClickListener(this);

        aboutButton=(Button) findViewById(R.id.about);
        aboutButton.setOnClickListener(this);
    }





        @Override
        public void onClick(android.view.View v) {


            RadioGroup drinkGroup = (RadioGroup) findViewById(R.id.drinks);

            switch (drinkGroup.getCheckedRadioButtonId()) {
            case R.id.coffee:
                drinkType.setText("coffee");
                subtotal=2.50;
                break;
            case R.id.cappuccino:
                drinkType.setText("cappuccinno");
                subtotal=4.50;
                break;
            case R.id.espresso:
                drinkType.setText("espresso");
                subtotal=4.50;
            case R.id.latte:
                drinkType.setText("latte");
                subtotal=4.50;
                break;
            case R.id.iced_latte:
                drinkType.setText("iced_latte");
                subtotal=5.00;
                break;
            }

            order.setText("Subtotal: "+subtotal+ "Tax: "+subtotal*.09+"Total: "+subtotal+subtotal*.09);

        }




    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Croom= (TextView) findViewById(R.id.cream);
        Sroom= (TextView) findViewById(R.id.sugar);
        go= (TextView) findViewById(R.id.go);
        flavor= (TextView) findViewById(R.id.flavoring);
        if (buttonView.getId() == R.id.cream) {
            if (isChecked) {
                Croom.setText("Room for cream");
            } else {
                Croom.setText("No room for cream");
            }
        }
            else if (buttonView.getId() == R.id.sugar) {
            if (isChecked) {
                Sroom.setText("Room for sugar");
            } else {
                Sroom.setText("No room for sugar");
            }

        }
            else if (buttonView.getId() == R.id.go) {
                if (isChecked) {
                    go.setText("To go");
                } else {
                    go.setText("Stay");
                }

            }
            else if (buttonView.getId() == R.id.chocolate) {
                if (isChecked) {
                    flavor.setText("Chocolate flavoring");
                }

            }

            else if (buttonView.getId() == R.id.cherry) {
                if (isChecked) {
                    flavor.setText("Cherry flavoring");
                }

            }

            else if (buttonView.getId() == R.id.vanilla) {
                if (isChecked) {
                    flavor.setText("Vanilla flavoring");
                }
            }


    aboutButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg7) {
        // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "*In boxer announcer voice*: "+"\n"+"Credit is given to me.", Toast.LENGTH_LONG).show();    
        }

    });




    sendButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg8) {
            String message;
            message = "Number of cups: "+DrinkNum+"\n"+go+"\n"
            +Croom+"\n"+Sroom+"\n"+"Drink type: "+drinkType+"\n"+"Flavoring: "+flavor
            +"\n"+"Price details: "+order+"\n"+"Thanks for ordering";


            Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show(); 
            }
        });
    }


}

我的第二个Toast消息没有连接我之前代码中的信息。此外,我不能只选择一个单选按钮,如果我点击任何其他按钮,他们也被选中。我该如何解决这两个问题?

1 个答案:

答案 0 :(得分:0)

对于您的第一个问题,您尝试将DrinkNum(类型为EditText(或View)的字符串连接到字符串。这是不可能的,如果它编译我会感到惊讶。

要解决您的第二个问题,您已将onClickListener附加到整个RadioGroup。这意味着如果点击了组内的RadioButton's中的任何一个,则会执行onClickListener's onClick()方法。