0
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 消息没有连接我之前代码中的信息。另外,我不能只选择一个单选按钮,如果我单击任何其他按钮,它们也会被选中。我该如何解决这两个问题?

4

1 回答 1

0

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

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

于 2013-10-04T03:07:57.453 回答