마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

라디오 버튼 두 개 합쳐진 값 출력

0 추천
라디오 그룹1에는 아메리카노, 카페라떼, 카푸치노

라디오 그룹2에는 스몰, 미디움, 라지

에 따른 가격을 각각 만들었는데

계산 버튼 클릭시 합쳐진 값을 출력하고 싶습니다.

계산 버튼 눌렀을 때 다 3000원이 나오게 되는데 어떤 부분이 잘못된 걸까요...

어떻게 고쳐야 제대로 된 값들이 나오게 될까요

(계산 부분에 볼드 처리 해놨습니다!)

 

 

/////////

 

import static android.widget.RadioGroup.*;

public class MainActivity extends AppCompatActivity {

    RadioGroup group, group2;
    RadioButton button1, button2, button3, button4,button5, button6;
    ImageView image;
    String str = "";
    RadioGroup radioGroup, radioGroup2;
    TextView tv,Result;
    Button button;
    private View.OnClickListener r;

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

        group = (RadioGroup) findViewById(R.id.radioGroup);
        button1 = (RadioButton) findViewById(R.id.Ame);
        button2 = (RadioButton) findViewById(R.id.Cafe);
        button3 = (RadioButton) findViewById(R.id.Capu);

        group2 = (RadioGroup) findViewById(R.id.radioGroup2);
        button4 = (RadioButton) findViewById(R.id.Small);
        button5 = (RadioButton) findViewById(R.id.Medium);
        button6 = (RadioButton) findViewById(R.id.Large);

        //이벤트 대상과 리스너(감지기) 등록
        button = (Button) findViewById(R.id.button);
        button1.setOnClickListener(radio_listener);
        button2.setOnClickListener(radio_listener);
        button3.setOnClickListener(radio_listener);
        button4.setOnClickListener(radio_listener);
        button5.setOnClickListener(radio_listener);
        button6.setOnClickListener(radio_listener);

        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
        image = (ImageView) findViewById(R.id.imageView);

        tv = (TextView) findViewById(R.id.textView);
        Result = (TextView) findViewById(R.id.Result);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.Ame) {
                    Toast.makeText(MainActivity.this, "아메리카노 선택", Toast.LENGTH_SHORT).show();
                } else if (checkedId == R.id.Cafe) {
                    Toast.makeText(MainActivity.this, "카페라떼 선택", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "카푸치노 선택", Toast.LENGTH_SHORT).show();

                }
            }
        });

        radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.Small) {
                    Toast.makeText(MainActivity.this, "Small 선택", Toast.LENGTH_SHORT).show();
                } else if (checkedId == R.id.Medium) {
                    Toast.makeText(MainActivity.this, "Medium 선택", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "Large 선택", Toast.LENGTH_SHORT).show();

                }

            }
        });
        

    }
    public void button(View view)
    {
        if(view == 1 && view == 4)
{
       Result.setText("1000원")
}
else if(view == 1 && view == 5)
{
       Result.setText("1500원")
}
else if(view == 1 && view == 6)
{
       Result.setText("2000원")
}
else if(view == 2 && view == 4)
{
       Result.setText("1500원")
}
else if(view == 2 && view == 5)
{
       Result.setText("2000원")
}
else if(view == 2 && view == 6)
{
       Result.setText("2500원")
}
else if(view == 3 && view == 4)
{
       Result.setText("2000원")
}
else if(view == 3 && view == 5)
{
       Result.setText("2500원")
}
else if
{
       Result.setText("3000원")
}
    
}
until5219 (140 포인트) 님이 2020년 5월 7일 질문
until5219님이 2020년 5월 7일 수정

1개의 답변

0 추천

button(View view) 메소드를 어디서 어떻게 호출하시는지 모르겠지만

이 메소드가 좀 이상하네요

 

view 변수는 하나들어오는데,

if(view == button1) { //<--view가 button1일때 if문 내 수행

            if(view == button4) //<--view는 button1이기때문에 항상 거짓
            {
                Result.setText("1000원");
            }
            else if(view == button4)  //<--view는 button1이기때문에 항상 거짓(글구 또 button4?)
            {
                Result.setText("1500원");
            }
            else  //<--결과적으로 button1을 누르면 항상 else만 수행함
            {
                Result.setText("2000원");
            }

}

//이하 else if도 마찬가지구요

요 메소드를 바꿔보심이 어떨까요

버닝 (4,880 포인트) 님이 2020년 5월 7일 답변
저부분을 잘못 올렸었어요 ㅠㅠ
처음ㅇ에는 이렇게 적었었어요
이 코드로 적었었는데 모든 값 다 3000이 나오더라고요
이 방법에서는 어떤점이 잘못되었는지도 봐주실수 있으실까요??
///////
if(view == 1 && view == 4)
{
       Result.setText("1000원")
}
else if(view == 1 && view == 5)
{
       Result.setText("1500원")
}
else if(view == 1 && view == 6)
{
       Result.setText("2000원")
}
else if(view == 2 && view == 4)
{
       Result.setText("1500원")
}
else if(view == 2 && view == 5)
{
       Result.setText("2000원")
}
else if(view == 2 && view == 6)
{
       Result.setText("2500원")
}
else if(view == 3 && view == 4)
{
       Result.setText("2000원")
}
else if(view == 3 && view == 5)
{
       Result.setText("2500원")
}
else if
{
       Result.setText("3000원")
}
...